ipv4.c 34 KB

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