ipv4.c 34 KB

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