ipv4.c 34 KB

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