ipv4.c 34 KB

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