ipv4.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * net/dccp/ipv4.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/dccp.h>
  14. #include <linux/icmp.h>
  15. #include <linux/module.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/random.h>
  18. #include <net/icmp.h>
  19. #include <net/inet_hashtables.h>
  20. #include <net/sock.h>
  21. #include <net/tcp_states.h>
  22. #include <net/xfrm.h>
  23. #include "ccid.h"
  24. #include "dccp.h"
  25. struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
  26. .lhash_lock = RW_LOCK_UNLOCKED,
  27. .lhash_users = ATOMIC_INIT(0),
  28. .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
  29. .portalloc_lock = SPIN_LOCK_UNLOCKED,
  30. .port_rover = 1024 - 1,
  31. };
  32. EXPORT_SYMBOL_GPL(dccp_hashinfo);
  33. static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
  34. {
  35. return inet_csk_get_port(&dccp_hashinfo, sk, snum);
  36. }
  37. static void dccp_v4_hash(struct sock *sk)
  38. {
  39. inet_hash(&dccp_hashinfo, sk);
  40. }
  41. static void dccp_v4_unhash(struct sock *sk)
  42. {
  43. inet_unhash(&dccp_hashinfo, sk);
  44. }
  45. /* called with local bh disabled */
  46. static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
  47. struct inet_timewait_sock **twp)
  48. {
  49. struct inet_sock *inet = inet_sk(sk);
  50. const u32 daddr = inet->rcv_saddr;
  51. const u32 saddr = inet->daddr;
  52. const int dif = sk->sk_bound_dev_if;
  53. INET_ADDR_COOKIE(acookie, saddr, daddr)
  54. const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
  55. const int hash = inet_ehashfn(daddr, lport, saddr, inet->dport,
  56. dccp_hashinfo.ehash_size);
  57. struct inet_ehash_bucket *head = &dccp_hashinfo.ehash[hash];
  58. const struct sock *sk2;
  59. const struct hlist_node *node;
  60. struct inet_timewait_sock *tw;
  61. write_lock(&head->lock);
  62. /* Check TIME-WAIT sockets first. */
  63. sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
  64. tw = inet_twsk(sk2);
  65. if (INET_TW_MATCH(sk2, acookie, saddr, daddr, ports, dif))
  66. goto not_unique;
  67. }
  68. tw = NULL;
  69. /* And established part... */
  70. sk_for_each(sk2, node, &head->chain) {
  71. if (INET_MATCH(sk2, acookie, saddr, daddr, ports, dif))
  72. goto not_unique;
  73. }
  74. /* Must record num and sport now. Otherwise we will see
  75. * in hash table socket with a funny identity. */
  76. inet->num = lport;
  77. inet->sport = htons(lport);
  78. sk->sk_hashent = hash;
  79. BUG_TRAP(sk_unhashed(sk));
  80. __sk_add_node(sk, &head->chain);
  81. sock_prot_inc_use(sk->sk_prot);
  82. write_unlock(&head->lock);
  83. if (twp != NULL) {
  84. *twp = tw;
  85. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  86. } else if (tw != NULL) {
  87. /* Silly. Should hash-dance instead... */
  88. inet_twsk_deschedule(tw, &dccp_death_row);
  89. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  90. inet_twsk_put(tw);
  91. }
  92. return 0;
  93. not_unique:
  94. write_unlock(&head->lock);
  95. return -EADDRNOTAVAIL;
  96. }
  97. /*
  98. * Bind a port for a connect operation and hash it.
  99. */
  100. static int dccp_v4_hash_connect(struct sock *sk)
  101. {
  102. const unsigned short snum = inet_sk(sk)->num;
  103. struct inet_bind_hashbucket *head;
  104. struct inet_bind_bucket *tb;
  105. int ret;
  106. if (snum == 0) {
  107. int rover;
  108. int low = sysctl_local_port_range[0];
  109. int high = sysctl_local_port_range[1];
  110. int remaining = (high - low) + 1;
  111. struct hlist_node *node;
  112. struct inet_timewait_sock *tw = NULL;
  113. local_bh_disable();
  114. /* TODO. Actually it is not so bad idea to remove
  115. * dccp_hashinfo.portalloc_lock before next submission to
  116. * Linus.
  117. * As soon as we touch this place at all it is time to think.
  118. *
  119. * Now it protects single _advisory_ variable
  120. * dccp_hashinfo.port_rover, hence it is mostly useless.
  121. * Code will work nicely if we just delete it, but
  122. * I am afraid in contented case it will work not better or
  123. * even worse: another cpu just will hit the same bucket
  124. * and spin there.
  125. * So some cpu salt could remove both contention and
  126. * memory pingpong. Any ideas how to do this in a nice way?
  127. */
  128. spin_lock(&dccp_hashinfo.portalloc_lock);
  129. rover = dccp_hashinfo.port_rover;
  130. do {
  131. rover++;
  132. if ((rover < low) || (rover > high))
  133. rover = low;
  134. head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
  135. dccp_hashinfo.bhash_size)];
  136. spin_lock(&head->lock);
  137. /* Does not bother with rcv_saddr checks,
  138. * because the established check is already
  139. * unique enough.
  140. */
  141. inet_bind_bucket_for_each(tb, node, &head->chain) {
  142. if (tb->port == rover) {
  143. BUG_TRAP(!hlist_empty(&tb->owners));
  144. if (tb->fastreuse >= 0)
  145. goto next_port;
  146. if (!__dccp_v4_check_established(sk,
  147. rover,
  148. &tw))
  149. goto ok;
  150. goto next_port;
  151. }
  152. }
  153. tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
  154. head, rover);
  155. if (tb == NULL) {
  156. spin_unlock(&head->lock);
  157. break;
  158. }
  159. tb->fastreuse = -1;
  160. goto ok;
  161. next_port:
  162. spin_unlock(&head->lock);
  163. } while (--remaining > 0);
  164. dccp_hashinfo.port_rover = rover;
  165. spin_unlock(&dccp_hashinfo.portalloc_lock);
  166. local_bh_enable();
  167. return -EADDRNOTAVAIL;
  168. ok:
  169. /* All locks still held and bhs disabled */
  170. dccp_hashinfo.port_rover = rover;
  171. spin_unlock(&dccp_hashinfo.portalloc_lock);
  172. inet_bind_hash(sk, tb, rover);
  173. if (sk_unhashed(sk)) {
  174. inet_sk(sk)->sport = htons(rover);
  175. __inet_hash(&dccp_hashinfo, sk, 0);
  176. }
  177. spin_unlock(&head->lock);
  178. if (tw != NULL) {
  179. inet_twsk_deschedule(tw, &dccp_death_row);
  180. inet_twsk_put(tw);
  181. }
  182. ret = 0;
  183. goto out;
  184. }
  185. head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
  186. dccp_hashinfo.bhash_size)];
  187. tb = inet_csk(sk)->icsk_bind_hash;
  188. spin_lock_bh(&head->lock);
  189. if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
  190. __inet_hash(&dccp_hashinfo, sk, 0);
  191. spin_unlock_bh(&head->lock);
  192. return 0;
  193. } else {
  194. spin_unlock(&head->lock);
  195. /* No definite answer... Walk to established hash table */
  196. ret = __dccp_v4_check_established(sk, snum, NULL);
  197. out:
  198. local_bh_enable();
  199. return ret;
  200. }
  201. }
  202. static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
  203. int addr_len)
  204. {
  205. struct inet_sock *inet = inet_sk(sk);
  206. struct dccp_sock *dp = dccp_sk(sk);
  207. const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  208. struct rtable *rt;
  209. u32 daddr, nexthop;
  210. int tmp;
  211. int err;
  212. dp->dccps_role = DCCP_ROLE_CLIENT;
  213. if (addr_len < sizeof(struct sockaddr_in))
  214. return -EINVAL;
  215. if (usin->sin_family != AF_INET)
  216. return -EAFNOSUPPORT;
  217. nexthop = daddr = usin->sin_addr.s_addr;
  218. if (inet->opt != NULL && inet->opt->srr) {
  219. if (daddr == 0)
  220. return -EINVAL;
  221. nexthop = inet->opt->faddr;
  222. }
  223. tmp = ip_route_connect(&rt, nexthop, inet->saddr,
  224. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  225. IPPROTO_DCCP,
  226. inet->sport, usin->sin_port, sk);
  227. if (tmp < 0)
  228. return tmp;
  229. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  230. ip_rt_put(rt);
  231. return -ENETUNREACH;
  232. }
  233. if (inet->opt == NULL || !inet->opt->srr)
  234. daddr = rt->rt_dst;
  235. if (inet->saddr == 0)
  236. inet->saddr = rt->rt_src;
  237. inet->rcv_saddr = inet->saddr;
  238. inet->dport = usin->sin_port;
  239. inet->daddr = daddr;
  240. dp->dccps_ext_header_len = 0;
  241. if (inet->opt != NULL)
  242. dp->dccps_ext_header_len = inet->opt->optlen;
  243. /*
  244. * Socket identity is still unknown (sport may be zero).
  245. * However we set state to DCCP_REQUESTING and not releasing socket
  246. * lock select source port, enter ourselves into the hash tables and
  247. * complete initialization after this.
  248. */
  249. dccp_set_state(sk, DCCP_REQUESTING);
  250. err = dccp_v4_hash_connect(sk);
  251. if (err != 0)
  252. goto failure;
  253. err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
  254. if (err != 0)
  255. goto failure;
  256. /* OK, now commit destination to socket. */
  257. sk_setup_caps(sk, &rt->u.dst);
  258. dp->dccps_gar =
  259. dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
  260. inet->daddr,
  261. inet->sport,
  262. usin->sin_port);
  263. dccp_update_gss(sk, dp->dccps_iss);
  264. /*
  265. * SWL and AWL are initially adjusted so that they are not less than
  266. * the initial Sequence Numbers received and sent, respectively:
  267. * SWL := max(GSR + 1 - floor(W/4), ISR),
  268. * AWL := max(GSS - W' + 1, ISS).
  269. * These adjustments MUST be applied only at the beginning of the
  270. * connection.
  271. */
  272. dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
  273. inet->id = dp->dccps_iss ^ jiffies;
  274. err = dccp_connect(sk);
  275. rt = NULL;
  276. if (err != 0)
  277. goto failure;
  278. out:
  279. return err;
  280. failure:
  281. /*
  282. * This unhashes the socket and releases the local port, if necessary.
  283. */
  284. dccp_set_state(sk, DCCP_CLOSED);
  285. ip_rt_put(rt);
  286. sk->sk_route_caps = 0;
  287. inet->dport = 0;
  288. goto out;
  289. }
  290. /*
  291. * This routine does path mtu discovery as defined in RFC1191.
  292. */
  293. static inline void dccp_do_pmtu_discovery(struct sock *sk,
  294. const struct iphdr *iph,
  295. u32 mtu)
  296. {
  297. struct dst_entry *dst;
  298. const struct inet_sock *inet = inet_sk(sk);
  299. const struct dccp_sock *dp = dccp_sk(sk);
  300. /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
  301. * send out by Linux are always < 576bytes so they should go through
  302. * unfragmented).
  303. */
  304. if (sk->sk_state == DCCP_LISTEN)
  305. return;
  306. /* We don't check in the destentry if pmtu discovery is forbidden
  307. * on this route. We just assume that no packet_to_big packets
  308. * are send back when pmtu discovery is not active.
  309. * There is a small race when the user changes this flag in the
  310. * route, but I think that's acceptable.
  311. */
  312. if ((dst = __sk_dst_check(sk, 0)) == NULL)
  313. return;
  314. dst->ops->update_pmtu(dst, mtu);
  315. /* Something is about to be wrong... Remember soft error
  316. * for the case, if this connection will not able to recover.
  317. */
  318. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  319. sk->sk_err_soft = EMSGSIZE;
  320. mtu = dst_mtu(dst);
  321. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  322. dp->dccps_pmtu_cookie > mtu) {
  323. dccp_sync_mss(sk, mtu);
  324. /*
  325. * From: draft-ietf-dccp-spec-11.txt
  326. *
  327. * DCCP-Sync packets are the best choice for upward
  328. * probing, since DCCP-Sync probes do not risk application
  329. * data loss.
  330. */
  331. dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
  332. } /* else let the usual retransmit timer handle it */
  333. }
  334. static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
  335. {
  336. int err;
  337. struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
  338. const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
  339. sizeof(struct dccp_hdr_ext) +
  340. sizeof(struct dccp_hdr_ack_bits);
  341. struct sk_buff *skb;
  342. if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
  343. return;
  344. skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
  345. if (skb == NULL)
  346. return;
  347. /* Reserve space for headers. */
  348. skb_reserve(skb, MAX_DCCP_HEADER);
  349. skb->dst = dst_clone(rxskb->dst);
  350. skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
  351. dh = dccp_hdr(skb);
  352. memset(dh, 0, dccp_hdr_ack_len);
  353. /* Build DCCP header and checksum it. */
  354. dh->dccph_type = DCCP_PKT_ACK;
  355. dh->dccph_sport = rxdh->dccph_dport;
  356. dh->dccph_dport = rxdh->dccph_sport;
  357. dh->dccph_doff = dccp_hdr_ack_len / 4;
  358. dh->dccph_x = 1;
  359. dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
  360. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
  361. DCCP_SKB_CB(rxskb)->dccpd_seq);
  362. bh_lock_sock(dccp_ctl_socket->sk);
  363. err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
  364. rxskb->nh.iph->daddr,
  365. rxskb->nh.iph->saddr, NULL);
  366. bh_unlock_sock(dccp_ctl_socket->sk);
  367. if (err == NET_XMIT_CN || err == 0) {
  368. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  369. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  370. }
  371. }
  372. static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
  373. struct request_sock *req)
  374. {
  375. dccp_v4_ctl_send_ack(skb);
  376. }
  377. static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
  378. struct dst_entry *dst)
  379. {
  380. int err = -1;
  381. struct sk_buff *skb;
  382. /* First, grab a route. */
  383. if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
  384. goto out;
  385. skb = dccp_make_response(sk, dst, req);
  386. if (skb != NULL) {
  387. const struct inet_request_sock *ireq = inet_rsk(req);
  388. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  389. ireq->rmt_addr,
  390. ireq->opt);
  391. if (err == NET_XMIT_CN)
  392. err = 0;
  393. }
  394. out:
  395. dst_release(dst);
  396. return err;
  397. }
  398. /*
  399. * This routine is called by the ICMP module when it gets some sort of error
  400. * condition. If err < 0 then the socket should be closed and the error
  401. * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
  402. * After adjustment header points to the first 8 bytes of the tcp header. We
  403. * need to find the appropriate port.
  404. *
  405. * The locking strategy used here is very "optimistic". When someone else
  406. * accesses the socket the ICMP is just dropped and for some paths there is no
  407. * check at all. A more general error queue to queue errors for later handling
  408. * is probably better.
  409. */
  410. void dccp_v4_err(struct sk_buff *skb, u32 info)
  411. {
  412. const struct iphdr *iph = (struct iphdr *)skb->data;
  413. const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
  414. (iph->ihl << 2));
  415. struct dccp_sock *dp;
  416. struct inet_sock *inet;
  417. const int type = skb->h.icmph->type;
  418. const int code = skb->h.icmph->code;
  419. struct sock *sk;
  420. __u64 seq;
  421. int err;
  422. if (skb->len < (iph->ihl << 2) + 8) {
  423. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  424. return;
  425. }
  426. sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
  427. iph->saddr, dh->dccph_sport, inet_iif(skb));
  428. if (sk == NULL) {
  429. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  430. return;
  431. }
  432. if (sk->sk_state == DCCP_TIME_WAIT) {
  433. inet_twsk_put((struct inet_timewait_sock *)sk);
  434. return;
  435. }
  436. bh_lock_sock(sk);
  437. /* If too many ICMPs get dropped on busy
  438. * servers this needs to be solved differently.
  439. */
  440. if (sock_owned_by_user(sk))
  441. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  442. if (sk->sk_state == DCCP_CLOSED)
  443. goto out;
  444. dp = dccp_sk(sk);
  445. seq = dccp_hdr_seq(skb);
  446. if (sk->sk_state != DCCP_LISTEN &&
  447. !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
  448. NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
  449. goto out;
  450. }
  451. switch (type) {
  452. case ICMP_SOURCE_QUENCH:
  453. /* Just silently ignore these. */
  454. goto out;
  455. case ICMP_PARAMETERPROB:
  456. err = EPROTO;
  457. break;
  458. case ICMP_DEST_UNREACH:
  459. if (code > NR_ICMP_UNREACH)
  460. goto out;
  461. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  462. if (!sock_owned_by_user(sk))
  463. dccp_do_pmtu_discovery(sk, iph, info);
  464. goto out;
  465. }
  466. err = icmp_err_convert[code].errno;
  467. break;
  468. case ICMP_TIME_EXCEEDED:
  469. err = EHOSTUNREACH;
  470. break;
  471. default:
  472. goto out;
  473. }
  474. switch (sk->sk_state) {
  475. struct request_sock *req , **prev;
  476. case DCCP_LISTEN:
  477. if (sock_owned_by_user(sk))
  478. goto out;
  479. req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
  480. iph->daddr, iph->saddr);
  481. if (!req)
  482. goto out;
  483. /*
  484. * ICMPs are not backlogged, hence we cannot get an established
  485. * socket here.
  486. */
  487. BUG_TRAP(!req->sk);
  488. if (seq != dccp_rsk(req)->dreq_iss) {
  489. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  490. goto out;
  491. }
  492. /*
  493. * Still in RESPOND, just remove it silently.
  494. * There is no good way to pass the error to the newly
  495. * created socket, and POSIX does not want network
  496. * errors returned from accept().
  497. */
  498. inet_csk_reqsk_queue_drop(sk, req, prev);
  499. goto out;
  500. case DCCP_REQUESTING:
  501. case DCCP_RESPOND:
  502. if (!sock_owned_by_user(sk)) {
  503. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  504. sk->sk_err = err;
  505. sk->sk_error_report(sk);
  506. dccp_done(sk);
  507. } else
  508. sk->sk_err_soft = err;
  509. goto out;
  510. }
  511. /* If we've already connected we will keep trying
  512. * until we time out, or the user gives up.
  513. *
  514. * rfc1122 4.2.3.9 allows to consider as hard errors
  515. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  516. * but it is obsoleted by pmtu discovery).
  517. *
  518. * Note, that in modern internet, where routing is unreliable
  519. * and in each dark corner broken firewalls sit, sending random
  520. * errors ordered by their masters even this two messages finally lose
  521. * their original sense (even Linux sends invalid PORT_UNREACHs)
  522. *
  523. * Now we are in compliance with RFCs.
  524. * --ANK (980905)
  525. */
  526. inet = inet_sk(sk);
  527. if (!sock_owned_by_user(sk) && inet->recverr) {
  528. sk->sk_err = err;
  529. sk->sk_error_report(sk);
  530. } else /* Only an error on timeout */
  531. sk->sk_err_soft = err;
  532. out:
  533. bh_unlock_sock(sk);
  534. sock_put(sk);
  535. }
  536. int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
  537. {
  538. struct sk_buff *skb;
  539. /*
  540. * FIXME: what if rebuild_header fails?
  541. * Should we be doing a rebuild_header here?
  542. */
  543. int err = inet_sk_rebuild_header(sk);
  544. if (err != 0)
  545. return err;
  546. skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
  547. if (skb != NULL) {
  548. const struct inet_sock *inet = inet_sk(sk);
  549. err = ip_build_and_send_pkt(skb, sk,
  550. inet->saddr, inet->daddr, NULL);
  551. if (err == NET_XMIT_CN)
  552. err = 0;
  553. }
  554. return err;
  555. }
  556. static inline u64 dccp_v4_init_sequence(const struct sock *sk,
  557. const struct sk_buff *skb)
  558. {
  559. return secure_dccp_sequence_number(skb->nh.iph->daddr,
  560. skb->nh.iph->saddr,
  561. dccp_hdr(skb)->dccph_dport,
  562. dccp_hdr(skb)->dccph_sport);
  563. }
  564. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  565. {
  566. struct inet_request_sock *ireq;
  567. struct dccp_sock dp;
  568. struct request_sock *req;
  569. struct dccp_request_sock *dreq;
  570. const __u32 saddr = skb->nh.iph->saddr;
  571. const __u32 daddr = skb->nh.iph->daddr;
  572. struct dst_entry *dst = NULL;
  573. /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
  574. if (((struct rtable *)skb->dst)->rt_flags &
  575. (RTCF_BROADCAST | RTCF_MULTICAST))
  576. goto drop;
  577. /*
  578. * TW buckets are converted to open requests without
  579. * limitations, they conserve resources and peer is
  580. * evidently real one.
  581. */
  582. if (inet_csk_reqsk_queue_is_full(sk))
  583. goto drop;
  584. /*
  585. * Accept backlog is full. If we have already queued enough
  586. * of warm entries in syn queue, drop request. It is better than
  587. * clogging syn queue with openreqs with exponentially increasing
  588. * timeout.
  589. */
  590. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  591. goto drop;
  592. req = reqsk_alloc(sk->sk_prot->rsk_prot);
  593. if (req == NULL)
  594. goto drop;
  595. /* FIXME: process options */
  596. dccp_openreq_init(req, &dp, skb);
  597. ireq = inet_rsk(req);
  598. ireq->loc_addr = daddr;
  599. ireq->rmt_addr = saddr;
  600. /* FIXME: Merge Aristeu's option parsing code when ready */
  601. req->rcv_wnd = 100; /* Fake, option parsing will get the
  602. right value */
  603. ireq->opt = NULL;
  604. /*
  605. * Step 3: Process LISTEN state
  606. *
  607. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  608. *
  609. * In fact we defer setting S.GSR, S.SWL, S.SWH to
  610. * dccp_create_openreq_child.
  611. */
  612. dreq = dccp_rsk(req);
  613. dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq;
  614. dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
  615. dreq->dreq_service = dccp_hdr_request(skb)->dccph_req_service;
  616. if (dccp_v4_send_response(sk, req, dst))
  617. goto drop_and_free;
  618. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  619. return 0;
  620. drop_and_free:
  621. /*
  622. * FIXME: should be reqsk_free after implementing req->rsk_ops
  623. */
  624. __reqsk_free(req);
  625. drop:
  626. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  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_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  857. dccp_v4_ctl_send_reset(skb);
  858. discard:
  859. kfree_skb(skb);
  860. return 0;
  861. }
  862. static inline int dccp_invalid_packet(struct sk_buff *skb)
  863. {
  864. const struct dccp_hdr *dh;
  865. if (skb->pkt_type != PACKET_HOST)
  866. return 1;
  867. if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
  868. LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
  869. return 1;
  870. }
  871. dh = dccp_hdr(skb);
  872. /* If the packet type is not understood, drop packet and return */
  873. if (dh->dccph_type >= DCCP_PKT_INVALID) {
  874. LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
  875. return 1;
  876. }
  877. /*
  878. * If P.Data Offset is too small for packet type, or too large for
  879. * packet, drop packet and return
  880. */
  881. if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
  882. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
  883. "too small 1\n",
  884. dh->dccph_doff);
  885. return 1;
  886. }
  887. if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
  888. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
  889. "too small 2\n",
  890. dh->dccph_doff);
  891. return 1;
  892. }
  893. dh = dccp_hdr(skb);
  894. /*
  895. * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
  896. * has short sequence numbers), drop packet and return
  897. */
  898. if (dh->dccph_x == 0 &&
  899. dh->dccph_type != DCCP_PKT_DATA &&
  900. dh->dccph_type != DCCP_PKT_ACK &&
  901. dh->dccph_type != DCCP_PKT_DATAACK) {
  902. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
  903. "nor DataAck and P.X == 0\n",
  904. dccp_packet_name(dh->dccph_type));
  905. return 1;
  906. }
  907. /* If the header checksum is incorrect, drop packet and return */
  908. if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
  909. skb->nh.iph->daddr) < 0) {
  910. LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is "
  911. "incorrect\n");
  912. return 1;
  913. }
  914. return 0;
  915. }
  916. /* this is called when real data arrives */
  917. int dccp_v4_rcv(struct sk_buff *skb)
  918. {
  919. const struct dccp_hdr *dh;
  920. struct sock *sk;
  921. int rc;
  922. /* Step 1: Check header basics: */
  923. if (dccp_invalid_packet(skb))
  924. goto discard_it;
  925. dh = dccp_hdr(skb);
  926. #if 0
  927. /*
  928. * Use something like this to simulate some DATA/DATAACK loss to test
  929. * dccp_ackpkts_add, you'll get something like this on a session that
  930. * sends 10 DATA/DATAACK packets:
  931. *
  932. * ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1|
  933. *
  934. * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet
  935. * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets
  936. * with the same state
  937. * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet
  938. *
  939. * So...
  940. *
  941. * 281473596467422 was received
  942. * 281473596467421 was not received
  943. * 281473596467420 was received
  944. * 281473596467419 was not received
  945. * 281473596467418 was received
  946. * 281473596467417 was not received
  947. * 281473596467416 was received
  948. * 281473596467415 was not received
  949. * 281473596467414 was received
  950. * 281473596467413 was received (this one was the 3way handshake
  951. * RESPONSE)
  952. *
  953. */
  954. if (dh->dccph_type == DCCP_PKT_DATA ||
  955. dh->dccph_type == DCCP_PKT_DATAACK) {
  956. static int discard = 0;
  957. if (discard) {
  958. discard = 0;
  959. goto discard_it;
  960. }
  961. discard = 1;
  962. }
  963. #endif
  964. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
  965. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  966. dccp_pr_debug("%8.8s "
  967. "src=%u.%u.%u.%u@%-5d "
  968. "dst=%u.%u.%u.%u@%-5d seq=%llu",
  969. dccp_packet_name(dh->dccph_type),
  970. NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
  971. NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
  972. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
  973. if (dccp_packet_without_ack(skb)) {
  974. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  975. dccp_pr_debug_cat("\n");
  976. } else {
  977. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  978. dccp_pr_debug_cat(", ack=%llu\n",
  979. (unsigned long long)
  980. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  981. }
  982. /* Step 2:
  983. * Look up flow ID in table and get corresponding socket */
  984. sk = __inet_lookup(&dccp_hashinfo,
  985. skb->nh.iph->saddr, dh->dccph_sport,
  986. skb->nh.iph->daddr, ntohs(dh->dccph_dport),
  987. inet_iif(skb));
  988. /*
  989. * Step 2:
  990. * If no socket ...
  991. * Generate Reset(No Connection) unless P.type == Reset
  992. * Drop packet and return
  993. */
  994. if (sk == NULL) {
  995. dccp_pr_debug("failed to look up flow ID in table and "
  996. "get corresponding socket\n");
  997. goto no_dccp_socket;
  998. }
  999. /*
  1000. * Step 2:
  1001. * ... or S.state == TIMEWAIT,
  1002. * Generate Reset(No Connection) unless P.type == Reset
  1003. * Drop packet and return
  1004. */
  1005. if (sk->sk_state == DCCP_TIME_WAIT) {
  1006. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
  1007. "do_time_wait\n");
  1008. goto do_time_wait;
  1009. }
  1010. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  1011. dccp_pr_debug("xfrm4_policy_check failed\n");
  1012. goto discard_and_relse;
  1013. }
  1014. if (sk_filter(sk, skb, 0)) {
  1015. dccp_pr_debug("sk_filter failed\n");
  1016. goto discard_and_relse;
  1017. }
  1018. skb->dev = NULL;
  1019. bh_lock_sock(sk);
  1020. rc = 0;
  1021. if (!sock_owned_by_user(sk))
  1022. rc = dccp_v4_do_rcv(sk, skb);
  1023. else
  1024. sk_add_backlog(sk, skb);
  1025. bh_unlock_sock(sk);
  1026. sock_put(sk);
  1027. return rc;
  1028. no_dccp_socket:
  1029. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1030. goto discard_it;
  1031. /*
  1032. * Step 2:
  1033. * Generate Reset(No Connection) unless P.type == Reset
  1034. * Drop packet and return
  1035. */
  1036. if (dh->dccph_type != DCCP_PKT_RESET) {
  1037. DCCP_SKB_CB(skb)->dccpd_reset_code =
  1038. DCCP_RESET_CODE_NO_CONNECTION;
  1039. dccp_v4_ctl_send_reset(skb);
  1040. }
  1041. discard_it:
  1042. /* Discard frame. */
  1043. kfree_skb(skb);
  1044. return 0;
  1045. discard_and_relse:
  1046. sock_put(sk);
  1047. goto discard_it;
  1048. do_time_wait:
  1049. inet_twsk_put((struct inet_timewait_sock *)sk);
  1050. goto no_dccp_socket;
  1051. }
  1052. static int dccp_v4_init_sock(struct sock *sk)
  1053. {
  1054. struct dccp_sock *dp = dccp_sk(sk);
  1055. static int dccp_ctl_socket_init = 1;
  1056. dccp_options_init(&dp->dccps_options);
  1057. do_gettimeofday(&dp->dccps_epoch);
  1058. if (dp->dccps_options.dccpo_send_ack_vector) {
  1059. dp->dccps_hc_rx_ackpkts =
  1060. dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
  1061. GFP_KERNEL);
  1062. if (dp->dccps_hc_rx_ackpkts == NULL)
  1063. return -ENOMEM;
  1064. }
  1065. /*
  1066. * FIXME: We're hardcoding the CCID, and doing this at this point makes
  1067. * the listening (master) sock get CCID control blocks, which is not
  1068. * necessary, but for now, to not mess with the test userspace apps,
  1069. * lets leave it here, later the real solution is to do this in a
  1070. * setsockopt(CCIDs-I-want/accept). -acme
  1071. */
  1072. if (likely(!dccp_ctl_socket_init)) {
  1073. dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
  1074. sk);
  1075. dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
  1076. sk);
  1077. if (dp->dccps_hc_rx_ccid == NULL ||
  1078. dp->dccps_hc_tx_ccid == NULL) {
  1079. ccid_exit(dp->dccps_hc_rx_ccid, sk);
  1080. ccid_exit(dp->dccps_hc_tx_ccid, sk);
  1081. dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
  1082. dp->dccps_hc_rx_ackpkts = NULL;
  1083. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1084. return -ENOMEM;
  1085. }
  1086. } else
  1087. dccp_ctl_socket_init = 0;
  1088. dccp_init_xmit_timers(sk);
  1089. inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
  1090. sk->sk_state = DCCP_CLOSED;
  1091. sk->sk_write_space = dccp_write_space;
  1092. dp->dccps_mss_cache = 536;
  1093. dp->dccps_role = DCCP_ROLE_UNDEFINED;
  1094. return 0;
  1095. }
  1096. static int dccp_v4_destroy_sock(struct sock *sk)
  1097. {
  1098. struct dccp_sock *dp = dccp_sk(sk);
  1099. /*
  1100. * DCCP doesn't use sk_qrite_queue, just sk_send_head
  1101. * for retransmissions
  1102. */
  1103. if (sk->sk_send_head != NULL) {
  1104. kfree_skb(sk->sk_send_head);
  1105. sk->sk_send_head = NULL;
  1106. }
  1107. /* Clean up a referenced DCCP bind bucket. */
  1108. if (inet_csk(sk)->icsk_bind_hash != NULL)
  1109. inet_put_port(&dccp_hashinfo, sk);
  1110. ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
  1111. ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
  1112. dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
  1113. dp->dccps_hc_rx_ackpkts = NULL;
  1114. ccid_exit(dp->dccps_hc_rx_ccid, sk);
  1115. ccid_exit(dp->dccps_hc_tx_ccid, sk);
  1116. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1117. return 0;
  1118. }
  1119. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  1120. {
  1121. kfree(inet_rsk(req)->opt);
  1122. }
  1123. static struct request_sock_ops dccp_request_sock_ops = {
  1124. .family = PF_INET,
  1125. .obj_size = sizeof(struct dccp_request_sock),
  1126. .rtx_syn_ack = dccp_v4_send_response,
  1127. .send_ack = dccp_v4_reqsk_send_ack,
  1128. .destructor = dccp_v4_reqsk_destructor,
  1129. .send_reset = dccp_v4_ctl_send_reset,
  1130. };
  1131. struct proto dccp_v4_prot = {
  1132. .name = "DCCP",
  1133. .owner = THIS_MODULE,
  1134. .close = dccp_close,
  1135. .connect = dccp_v4_connect,
  1136. .disconnect = dccp_disconnect,
  1137. .ioctl = dccp_ioctl,
  1138. .init = dccp_v4_init_sock,
  1139. .setsockopt = dccp_setsockopt,
  1140. .getsockopt = dccp_getsockopt,
  1141. .sendmsg = dccp_sendmsg,
  1142. .recvmsg = dccp_recvmsg,
  1143. .backlog_rcv = dccp_v4_do_rcv,
  1144. .hash = dccp_v4_hash,
  1145. .unhash = dccp_v4_unhash,
  1146. .accept = inet_csk_accept,
  1147. .get_port = dccp_v4_get_port,
  1148. .shutdown = dccp_shutdown,
  1149. .destroy = dccp_v4_destroy_sock,
  1150. .orphan_count = &dccp_orphan_count,
  1151. .max_header = MAX_DCCP_HEADER,
  1152. .obj_size = sizeof(struct dccp_sock),
  1153. .rsk_prot = &dccp_request_sock_ops,
  1154. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  1155. };