ipv4.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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/dccp.h>
  13. #include <linux/icmp.h>
  14. #include <linux/module.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/random.h>
  17. #include <net/icmp.h>
  18. #include <net/inet_common.h>
  19. #include <net/inet_hashtables.h>
  20. #include <net/inet_sock.h>
  21. #include <net/protocol.h>
  22. #include <net/sock.h>
  23. #include <net/timewait_sock.h>
  24. #include <net/tcp_states.h>
  25. #include <net/xfrm.h>
  26. #include "ackvec.h"
  27. #include "ccid.h"
  28. #include "dccp.h"
  29. #include "feat.h"
  30. /*
  31. * This is the global socket data structure used for responding to
  32. * the Out-of-the-blue (OOTB) packets. A control sock will be created
  33. * for this socket at the initialization time.
  34. */
  35. static struct socket *dccp_v4_ctl_socket;
  36. static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
  37. {
  38. return inet_csk_get_port(&dccp_hashinfo, sk, snum,
  39. inet_csk_bind_conflict);
  40. }
  41. int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  42. {
  43. struct inet_sock *inet = inet_sk(sk);
  44. struct dccp_sock *dp = dccp_sk(sk);
  45. const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  46. struct rtable *rt;
  47. __be32 daddr, nexthop;
  48. int tmp;
  49. int err;
  50. dp->dccps_role = DCCP_ROLE_CLIENT;
  51. if (addr_len < sizeof(struct sockaddr_in))
  52. return -EINVAL;
  53. if (usin->sin_family != AF_INET)
  54. return -EAFNOSUPPORT;
  55. nexthop = daddr = usin->sin_addr.s_addr;
  56. if (inet->opt != NULL && inet->opt->srr) {
  57. if (daddr == 0)
  58. return -EINVAL;
  59. nexthop = inet->opt->faddr;
  60. }
  61. tmp = ip_route_connect(&rt, nexthop, inet->saddr,
  62. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  63. IPPROTO_DCCP,
  64. inet->sport, usin->sin_port, sk, 1);
  65. if (tmp < 0)
  66. return tmp;
  67. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  68. ip_rt_put(rt);
  69. return -ENETUNREACH;
  70. }
  71. if (inet->opt == NULL || !inet->opt->srr)
  72. daddr = rt->rt_dst;
  73. if (inet->saddr == 0)
  74. inet->saddr = rt->rt_src;
  75. inet->rcv_saddr = inet->saddr;
  76. inet->dport = usin->sin_port;
  77. inet->daddr = daddr;
  78. inet_csk(sk)->icsk_ext_hdr_len = 0;
  79. if (inet->opt != NULL)
  80. inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
  81. /*
  82. * Socket identity is still unknown (sport may be zero).
  83. * However we set state to DCCP_REQUESTING and not releasing socket
  84. * lock select source port, enter ourselves into the hash tables and
  85. * complete initialization after this.
  86. */
  87. dccp_set_state(sk, DCCP_REQUESTING);
  88. err = inet_hash_connect(&dccp_death_row, sk);
  89. if (err != 0)
  90. goto failure;
  91. err = ip_route_newports(&rt, IPPROTO_DCCP, inet->sport, inet->dport,
  92. sk);
  93. if (err != 0)
  94. goto failure;
  95. /* OK, now commit destination to socket. */
  96. sk_setup_caps(sk, &rt->u.dst);
  97. dp->dccps_iss = secure_dccp_sequence_number(inet->saddr, inet->daddr,
  98. inet->sport, inet->dport);
  99. inet->id = dp->dccps_iss ^ jiffies;
  100. err = dccp_connect(sk);
  101. rt = NULL;
  102. if (err != 0)
  103. goto failure;
  104. out:
  105. return err;
  106. failure:
  107. /*
  108. * This unhashes the socket and releases the local port, if necessary.
  109. */
  110. dccp_set_state(sk, DCCP_CLOSED);
  111. ip_rt_put(rt);
  112. sk->sk_route_caps = 0;
  113. inet->dport = 0;
  114. goto out;
  115. }
  116. EXPORT_SYMBOL_GPL(dccp_v4_connect);
  117. /*
  118. * This routine does path mtu discovery as defined in RFC1191.
  119. */
  120. static inline void dccp_do_pmtu_discovery(struct sock *sk,
  121. const struct iphdr *iph,
  122. u32 mtu)
  123. {
  124. struct dst_entry *dst;
  125. const struct inet_sock *inet = inet_sk(sk);
  126. const struct dccp_sock *dp = dccp_sk(sk);
  127. /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
  128. * send out by Linux are always < 576bytes so they should go through
  129. * unfragmented).
  130. */
  131. if (sk->sk_state == DCCP_LISTEN)
  132. return;
  133. /* We don't check in the destentry if pmtu discovery is forbidden
  134. * on this route. We just assume that no packet_to_big packets
  135. * are send back when pmtu discovery is not active.
  136. * There is a small race when the user changes this flag in the
  137. * route, but I think that's acceptable.
  138. */
  139. if ((dst = __sk_dst_check(sk, 0)) == NULL)
  140. return;
  141. dst->ops->update_pmtu(dst, mtu);
  142. /* Something is about to be wrong... Remember soft error
  143. * for the case, if this connection will not able to recover.
  144. */
  145. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  146. sk->sk_err_soft = EMSGSIZE;
  147. mtu = dst_mtu(dst);
  148. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  149. inet_csk(sk)->icsk_pmtu_cookie > mtu) {
  150. dccp_sync_mss(sk, mtu);
  151. /*
  152. * From RFC 4340, sec. 14.1:
  153. *
  154. * DCCP-Sync packets are the best choice for upward
  155. * probing, since DCCP-Sync probes do not risk application
  156. * data loss.
  157. */
  158. dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
  159. } /* else let the usual retransmit timer handle it */
  160. }
  161. /*
  162. * This routine is called by the ICMP module when it gets some sort of error
  163. * condition. If err < 0 then the socket should be closed and the error
  164. * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
  165. * After adjustment header points to the first 8 bytes of the tcp header. We
  166. * need to find the appropriate port.
  167. *
  168. * The locking strategy used here is very "optimistic". When someone else
  169. * accesses the socket the ICMP is just dropped and for some paths there is no
  170. * check at all. A more general error queue to queue errors for later handling
  171. * is probably better.
  172. */
  173. static void dccp_v4_err(struct sk_buff *skb, u32 info)
  174. {
  175. const struct iphdr *iph = (struct iphdr *)skb->data;
  176. const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
  177. (iph->ihl << 2));
  178. struct dccp_sock *dp;
  179. struct inet_sock *inet;
  180. const int type = icmp_hdr(skb)->type;
  181. const int code = icmp_hdr(skb)->code;
  182. struct sock *sk;
  183. __u64 seq;
  184. int err;
  185. if (skb->len < (iph->ihl << 2) + 8) {
  186. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  187. return;
  188. }
  189. sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
  190. iph->saddr, dh->dccph_sport, inet_iif(skb));
  191. if (sk == NULL) {
  192. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  193. return;
  194. }
  195. if (sk->sk_state == DCCP_TIME_WAIT) {
  196. inet_twsk_put(inet_twsk(sk));
  197. return;
  198. }
  199. bh_lock_sock(sk);
  200. /* If too many ICMPs get dropped on busy
  201. * servers this needs to be solved differently.
  202. */
  203. if (sock_owned_by_user(sk))
  204. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  205. if (sk->sk_state == DCCP_CLOSED)
  206. goto out;
  207. dp = dccp_sk(sk);
  208. seq = dccp_hdr_seq(skb);
  209. if (sk->sk_state != DCCP_LISTEN &&
  210. !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
  211. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  212. goto out;
  213. }
  214. switch (type) {
  215. case ICMP_SOURCE_QUENCH:
  216. /* Just silently ignore these. */
  217. goto out;
  218. case ICMP_PARAMETERPROB:
  219. err = EPROTO;
  220. break;
  221. case ICMP_DEST_UNREACH:
  222. if (code > NR_ICMP_UNREACH)
  223. goto out;
  224. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  225. if (!sock_owned_by_user(sk))
  226. dccp_do_pmtu_discovery(sk, iph, info);
  227. goto out;
  228. }
  229. err = icmp_err_convert[code].errno;
  230. break;
  231. case ICMP_TIME_EXCEEDED:
  232. err = EHOSTUNREACH;
  233. break;
  234. default:
  235. goto out;
  236. }
  237. switch (sk->sk_state) {
  238. struct request_sock *req , **prev;
  239. case DCCP_LISTEN:
  240. if (sock_owned_by_user(sk))
  241. goto out;
  242. req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
  243. iph->daddr, iph->saddr);
  244. if (!req)
  245. goto out;
  246. /*
  247. * ICMPs are not backlogged, hence we cannot get an established
  248. * socket here.
  249. */
  250. BUG_TRAP(!req->sk);
  251. if (seq != dccp_rsk(req)->dreq_iss) {
  252. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  253. goto out;
  254. }
  255. /*
  256. * Still in RESPOND, just remove it silently.
  257. * There is no good way to pass the error to the newly
  258. * created socket, and POSIX does not want network
  259. * errors returned from accept().
  260. */
  261. inet_csk_reqsk_queue_drop(sk, req, prev);
  262. goto out;
  263. case DCCP_REQUESTING:
  264. case DCCP_RESPOND:
  265. if (!sock_owned_by_user(sk)) {
  266. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  267. sk->sk_err = err;
  268. sk->sk_error_report(sk);
  269. dccp_done(sk);
  270. } else
  271. sk->sk_err_soft = err;
  272. goto out;
  273. }
  274. /* If we've already connected we will keep trying
  275. * until we time out, or the user gives up.
  276. *
  277. * rfc1122 4.2.3.9 allows to consider as hard errors
  278. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  279. * but it is obsoleted by pmtu discovery).
  280. *
  281. * Note, that in modern internet, where routing is unreliable
  282. * and in each dark corner broken firewalls sit, sending random
  283. * errors ordered by their masters even this two messages finally lose
  284. * their original sense (even Linux sends invalid PORT_UNREACHs)
  285. *
  286. * Now we are in compliance with RFCs.
  287. * --ANK (980905)
  288. */
  289. inet = inet_sk(sk);
  290. if (!sock_owned_by_user(sk) && inet->recverr) {
  291. sk->sk_err = err;
  292. sk->sk_error_report(sk);
  293. } else /* Only an error on timeout */
  294. sk->sk_err_soft = err;
  295. out:
  296. bh_unlock_sock(sk);
  297. sock_put(sk);
  298. }
  299. static inline __sum16 dccp_v4_csum_finish(struct sk_buff *skb,
  300. __be32 src, __be32 dst)
  301. {
  302. return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
  303. }
  304. void dccp_v4_send_check(struct sock *sk, int unused, struct sk_buff *skb)
  305. {
  306. const struct inet_sock *inet = inet_sk(sk);
  307. struct dccp_hdr *dh = dccp_hdr(skb);
  308. dccp_csum_outgoing(skb);
  309. dh->dccph_checksum = dccp_v4_csum_finish(skb, inet->saddr, inet->daddr);
  310. }
  311. EXPORT_SYMBOL_GPL(dccp_v4_send_check);
  312. static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
  313. {
  314. return secure_dccp_sequence_number(ip_hdr(skb)->daddr,
  315. ip_hdr(skb)->saddr,
  316. dccp_hdr(skb)->dccph_dport,
  317. dccp_hdr(skb)->dccph_sport);
  318. }
  319. /*
  320. * The three way handshake has completed - we got a valid ACK or DATAACK -
  321. * now create the new socket.
  322. *
  323. * This is the equivalent of TCP's tcp_v4_syn_recv_sock
  324. */
  325. struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
  326. struct request_sock *req,
  327. struct dst_entry *dst)
  328. {
  329. struct inet_request_sock *ireq;
  330. struct inet_sock *newinet;
  331. struct sock *newsk;
  332. if (sk_acceptq_is_full(sk))
  333. goto exit_overflow;
  334. if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
  335. goto exit;
  336. newsk = dccp_create_openreq_child(sk, req, skb);
  337. if (newsk == NULL)
  338. goto exit;
  339. sk_setup_caps(newsk, dst);
  340. newinet = inet_sk(newsk);
  341. ireq = inet_rsk(req);
  342. newinet->daddr = ireq->rmt_addr;
  343. newinet->rcv_saddr = ireq->loc_addr;
  344. newinet->saddr = ireq->loc_addr;
  345. newinet->opt = ireq->opt;
  346. ireq->opt = NULL;
  347. newinet->mc_index = inet_iif(skb);
  348. newinet->mc_ttl = ip_hdr(skb)->ttl;
  349. newinet->id = jiffies;
  350. dccp_sync_mss(newsk, dst_mtu(dst));
  351. __inet_hash(&dccp_hashinfo, newsk, 0);
  352. __inet_inherit_port(&dccp_hashinfo, sk, newsk);
  353. return newsk;
  354. exit_overflow:
  355. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  356. exit:
  357. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  358. dst_release(dst);
  359. return NULL;
  360. }
  361. EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
  362. static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  363. {
  364. const struct dccp_hdr *dh = dccp_hdr(skb);
  365. const struct iphdr *iph = ip_hdr(skb);
  366. struct sock *nsk;
  367. struct request_sock **prev;
  368. /* Find possible connection requests. */
  369. struct request_sock *req = inet_csk_search_req(sk, &prev,
  370. dh->dccph_sport,
  371. iph->saddr, iph->daddr);
  372. if (req != NULL)
  373. return dccp_check_req(sk, skb, req, prev);
  374. nsk = inet_lookup_established(&dccp_hashinfo,
  375. iph->saddr, dh->dccph_sport,
  376. iph->daddr, dh->dccph_dport,
  377. inet_iif(skb));
  378. if (nsk != NULL) {
  379. if (nsk->sk_state != DCCP_TIME_WAIT) {
  380. bh_lock_sock(nsk);
  381. return nsk;
  382. }
  383. inet_twsk_put(inet_twsk(nsk));
  384. return NULL;
  385. }
  386. return sk;
  387. }
  388. static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
  389. struct sk_buff *skb)
  390. {
  391. struct rtable *rt;
  392. struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
  393. .nl_u = { .ip4_u =
  394. { .daddr = ip_hdr(skb)->saddr,
  395. .saddr = ip_hdr(skb)->daddr,
  396. .tos = RT_CONN_FLAGS(sk) } },
  397. .proto = sk->sk_protocol,
  398. .uli_u = { .ports =
  399. { .sport = dccp_hdr(skb)->dccph_dport,
  400. .dport = dccp_hdr(skb)->dccph_sport }
  401. }
  402. };
  403. security_skb_classify_flow(skb, &fl);
  404. if (ip_route_output_flow(&rt, &fl, sk, 0)) {
  405. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  406. return NULL;
  407. }
  408. return &rt->u.dst;
  409. }
  410. static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
  411. struct dst_entry *dst)
  412. {
  413. int err = -1;
  414. struct sk_buff *skb;
  415. /* First, grab a route. */
  416. if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
  417. goto out;
  418. skb = dccp_make_response(sk, dst, req);
  419. if (skb != NULL) {
  420. const struct inet_request_sock *ireq = inet_rsk(req);
  421. struct dccp_hdr *dh = dccp_hdr(skb);
  422. dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->loc_addr,
  423. ireq->rmt_addr);
  424. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  425. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  426. ireq->rmt_addr,
  427. ireq->opt);
  428. err = net_xmit_eval(err);
  429. }
  430. out:
  431. dst_release(dst);
  432. return err;
  433. }
  434. static void dccp_v4_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb)
  435. {
  436. int err;
  437. const struct iphdr *rxiph;
  438. struct sk_buff *skb;
  439. struct dst_entry *dst;
  440. /* Never send a reset in response to a reset. */
  441. if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
  442. return;
  443. if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
  444. return;
  445. dst = dccp_v4_route_skb(dccp_v4_ctl_socket->sk, rxskb);
  446. if (dst == NULL)
  447. return;
  448. skb = dccp_ctl_make_reset(dccp_v4_ctl_socket, rxskb);
  449. if (skb == NULL)
  450. goto out;
  451. rxiph = ip_hdr(rxskb);
  452. dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
  453. rxiph->daddr);
  454. skb->dst = dst_clone(dst);
  455. bh_lock_sock(dccp_v4_ctl_socket->sk);
  456. err = ip_build_and_send_pkt(skb, dccp_v4_ctl_socket->sk,
  457. rxiph->daddr, rxiph->saddr, NULL);
  458. bh_unlock_sock(dccp_v4_ctl_socket->sk);
  459. if (net_xmit_eval(err) == 0) {
  460. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  461. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  462. }
  463. out:
  464. dst_release(dst);
  465. }
  466. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  467. {
  468. kfree(inet_rsk(req)->opt);
  469. }
  470. static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
  471. .family = PF_INET,
  472. .obj_size = sizeof(struct dccp_request_sock),
  473. .rtx_syn_ack = dccp_v4_send_response,
  474. .send_ack = dccp_reqsk_send_ack,
  475. .destructor = dccp_v4_reqsk_destructor,
  476. .send_reset = dccp_v4_ctl_send_reset,
  477. };
  478. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  479. {
  480. struct inet_request_sock *ireq;
  481. struct request_sock *req;
  482. struct dccp_request_sock *dreq;
  483. const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
  484. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  485. /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
  486. if (((struct rtable *)skb->dst)->rt_flags &
  487. (RTCF_BROADCAST | RTCF_MULTICAST))
  488. return 0; /* discard, don't send a reset here */
  489. if (dccp_bad_service_code(sk, service)) {
  490. dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
  491. goto drop;
  492. }
  493. /*
  494. * TW buckets are converted to open requests without
  495. * limitations, they conserve resources and peer is
  496. * evidently real one.
  497. */
  498. dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  499. if (inet_csk_reqsk_queue_is_full(sk))
  500. goto drop;
  501. /*
  502. * Accept backlog is full. If we have already queued enough
  503. * of warm entries in syn queue, drop request. It is better than
  504. * clogging syn queue with openreqs with exponentially increasing
  505. * timeout.
  506. */
  507. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  508. goto drop;
  509. req = reqsk_alloc(&dccp_request_sock_ops);
  510. if (req == NULL)
  511. goto drop;
  512. if (dccp_parse_options(sk, skb))
  513. goto drop_and_free;
  514. dccp_reqsk_init(req, skb);
  515. if (security_inet_conn_request(sk, skb, req))
  516. goto drop_and_free;
  517. ireq = inet_rsk(req);
  518. ireq->loc_addr = ip_hdr(skb)->daddr;
  519. ireq->rmt_addr = ip_hdr(skb)->saddr;
  520. ireq->opt = NULL;
  521. /*
  522. * Step 3: Process LISTEN state
  523. *
  524. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  525. *
  526. * In fact we defer setting S.GSR, S.SWL, S.SWH to
  527. * dccp_create_openreq_child.
  528. */
  529. dreq = dccp_rsk(req);
  530. dreq->dreq_isr = dcb->dccpd_seq;
  531. dreq->dreq_iss = dccp_v4_init_sequence(skb);
  532. dreq->dreq_service = service;
  533. if (dccp_v4_send_response(sk, req, NULL))
  534. goto drop_and_free;
  535. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  536. return 0;
  537. drop_and_free:
  538. reqsk_free(req);
  539. drop:
  540. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  541. return -1;
  542. }
  543. EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
  544. int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  545. {
  546. struct dccp_hdr *dh = dccp_hdr(skb);
  547. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  548. if (dccp_rcv_established(sk, skb, dh, skb->len))
  549. goto reset;
  550. return 0;
  551. }
  552. /*
  553. * Step 3: Process LISTEN state
  554. * If P.type == Request or P contains a valid Init Cookie option,
  555. * (* Must scan the packet's options to check for Init
  556. * Cookies. Only Init Cookies are processed here,
  557. * however; other options are processed in Step 8. This
  558. * scan need only be performed if the endpoint uses Init
  559. * Cookies *)
  560. * (* Generate a new socket and switch to that socket *)
  561. * Set S := new socket for this port pair
  562. * S.state = RESPOND
  563. * Choose S.ISS (initial seqno) or set from Init Cookies
  564. * Initialize S.GAR := S.ISS
  565. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
  566. * Continue with S.state == RESPOND
  567. * (* A Response packet will be generated in Step 11 *)
  568. * Otherwise,
  569. * Generate Reset(No Connection) unless P.type == Reset
  570. * Drop packet and return
  571. *
  572. * NOTE: the check for the packet types is done in
  573. * dccp_rcv_state_process
  574. */
  575. if (sk->sk_state == DCCP_LISTEN) {
  576. struct sock *nsk = dccp_v4_hnd_req(sk, skb);
  577. if (nsk == NULL)
  578. goto discard;
  579. if (nsk != sk) {
  580. if (dccp_child_process(sk, nsk, skb))
  581. goto reset;
  582. return 0;
  583. }
  584. }
  585. if (dccp_rcv_state_process(sk, skb, dh, skb->len))
  586. goto reset;
  587. return 0;
  588. reset:
  589. dccp_v4_ctl_send_reset(sk, skb);
  590. discard:
  591. kfree_skb(skb);
  592. return 0;
  593. }
  594. EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
  595. /**
  596. * dccp_invalid_packet - check for malformed packets
  597. * Implements RFC 4340, 8.5: Step 1: Check header basics
  598. * Packets that fail these checks are ignored and do not receive Resets.
  599. */
  600. int dccp_invalid_packet(struct sk_buff *skb)
  601. {
  602. const struct dccp_hdr *dh;
  603. unsigned int cscov;
  604. if (skb->pkt_type != PACKET_HOST)
  605. return 1;
  606. /* If the packet is shorter than 12 bytes, drop packet and return */
  607. if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
  608. DCCP_WARN("pskb_may_pull failed\n");
  609. return 1;
  610. }
  611. dh = dccp_hdr(skb);
  612. /* If P.type is not understood, drop packet and return */
  613. if (dh->dccph_type >= DCCP_PKT_INVALID) {
  614. DCCP_WARN("invalid packet type\n");
  615. return 1;
  616. }
  617. /*
  618. * If P.Data Offset is too small for packet type, drop packet and return
  619. */
  620. if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
  621. DCCP_WARN("P.Data Offset(%u) too small\n", dh->dccph_doff);
  622. return 1;
  623. }
  624. /*
  625. * If P.Data Offset is too too large for packet, drop packet and return
  626. */
  627. if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
  628. DCCP_WARN("P.Data Offset(%u) too large\n", dh->dccph_doff);
  629. return 1;
  630. }
  631. /*
  632. * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
  633. * has short sequence numbers), drop packet and return
  634. */
  635. if (dh->dccph_type >= DCCP_PKT_DATA &&
  636. dh->dccph_type <= DCCP_PKT_DATAACK && dh->dccph_x == 0) {
  637. DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
  638. dccp_packet_name(dh->dccph_type));
  639. return 1;
  640. }
  641. /*
  642. * If P.CsCov is too large for the packet size, drop packet and return.
  643. * This must come _before_ checksumming (not as RFC 4340 suggests).
  644. */
  645. cscov = dccp_csum_coverage(skb);
  646. if (cscov > skb->len) {
  647. DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
  648. dh->dccph_cscov, skb->len);
  649. return 1;
  650. }
  651. /* If header checksum is incorrect, drop packet and return.
  652. * (This step is completed in the AF-dependent functions.) */
  653. skb->csum = skb_checksum(skb, 0, cscov, 0);
  654. return 0;
  655. }
  656. EXPORT_SYMBOL_GPL(dccp_invalid_packet);
  657. /* this is called when real data arrives */
  658. static int dccp_v4_rcv(struct sk_buff *skb)
  659. {
  660. const struct dccp_hdr *dh;
  661. const struct iphdr *iph;
  662. struct sock *sk;
  663. int min_cov;
  664. /* Step 1: Check header basics */
  665. if (dccp_invalid_packet(skb))
  666. goto discard_it;
  667. iph = ip_hdr(skb);
  668. /* Step 1: If header checksum is incorrect, drop packet and return */
  669. if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
  670. DCCP_WARN("dropped packet with invalid checksum\n");
  671. goto discard_it;
  672. }
  673. dh = dccp_hdr(skb);
  674. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
  675. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  676. dccp_pr_debug("%8.8s "
  677. "src=%u.%u.%u.%u@%-5d "
  678. "dst=%u.%u.%u.%u@%-5d seq=%llu",
  679. dccp_packet_name(dh->dccph_type),
  680. NIPQUAD(iph->saddr), ntohs(dh->dccph_sport),
  681. NIPQUAD(iph->daddr), ntohs(dh->dccph_dport),
  682. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
  683. if (dccp_packet_without_ack(skb)) {
  684. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  685. dccp_pr_debug_cat("\n");
  686. } else {
  687. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  688. dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
  689. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  690. }
  691. /* Step 2:
  692. * Look up flow ID in table and get corresponding socket */
  693. sk = __inet_lookup(&dccp_hashinfo,
  694. iph->saddr, dh->dccph_sport,
  695. iph->daddr, dh->dccph_dport, inet_iif(skb));
  696. /*
  697. * Step 2:
  698. * If no socket ...
  699. */
  700. if (sk == NULL) {
  701. dccp_pr_debug("failed to look up flow ID in table and "
  702. "get corresponding socket\n");
  703. goto no_dccp_socket;
  704. }
  705. /*
  706. * Step 2:
  707. * ... or S.state == TIMEWAIT,
  708. * Generate Reset(No Connection) unless P.type == Reset
  709. * Drop packet and return
  710. */
  711. if (sk->sk_state == DCCP_TIME_WAIT) {
  712. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
  713. inet_twsk_put(inet_twsk(sk));
  714. goto no_dccp_socket;
  715. }
  716. /*
  717. * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
  718. * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
  719. * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
  720. */
  721. min_cov = dccp_sk(sk)->dccps_pcrlen;
  722. if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
  723. dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
  724. dh->dccph_cscov, min_cov);
  725. /* FIXME: "Such packets SHOULD be reported using Data Dropped
  726. * options (Section 11.7) with Drop Code 0, Protocol
  727. * Constraints." */
  728. goto discard_and_relse;
  729. }
  730. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  731. goto discard_and_relse;
  732. nf_reset(skb);
  733. return sk_receive_skb(sk, skb, 1);
  734. no_dccp_socket:
  735. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  736. goto discard_it;
  737. /*
  738. * Step 2:
  739. * If no socket ...
  740. * Generate Reset(No Connection) unless P.type == Reset
  741. * Drop packet and return
  742. */
  743. if (dh->dccph_type != DCCP_PKT_RESET) {
  744. DCCP_SKB_CB(skb)->dccpd_reset_code =
  745. DCCP_RESET_CODE_NO_CONNECTION;
  746. dccp_v4_ctl_send_reset(sk, skb);
  747. }
  748. discard_it:
  749. kfree_skb(skb);
  750. return 0;
  751. discard_and_relse:
  752. sock_put(sk);
  753. goto discard_it;
  754. }
  755. static struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
  756. .queue_xmit = ip_queue_xmit,
  757. .send_check = dccp_v4_send_check,
  758. .rebuild_header = inet_sk_rebuild_header,
  759. .conn_request = dccp_v4_conn_request,
  760. .syn_recv_sock = dccp_v4_request_recv_sock,
  761. .net_header_len = sizeof(struct iphdr),
  762. .setsockopt = ip_setsockopt,
  763. .getsockopt = ip_getsockopt,
  764. .addr2sockaddr = inet_csk_addr2sockaddr,
  765. .sockaddr_len = sizeof(struct sockaddr_in),
  766. #ifdef CONFIG_COMPAT
  767. .compat_setsockopt = compat_ip_setsockopt,
  768. .compat_getsockopt = compat_ip_getsockopt,
  769. #endif
  770. };
  771. static int dccp_v4_init_sock(struct sock *sk)
  772. {
  773. static __u8 dccp_v4_ctl_sock_initialized;
  774. int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
  775. if (err == 0) {
  776. if (unlikely(!dccp_v4_ctl_sock_initialized))
  777. dccp_v4_ctl_sock_initialized = 1;
  778. inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
  779. }
  780. return err;
  781. }
  782. static struct timewait_sock_ops dccp_timewait_sock_ops = {
  783. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  784. };
  785. static struct proto dccp_v4_prot = {
  786. .name = "DCCP",
  787. .owner = THIS_MODULE,
  788. .close = dccp_close,
  789. .connect = dccp_v4_connect,
  790. .disconnect = dccp_disconnect,
  791. .ioctl = dccp_ioctl,
  792. .init = dccp_v4_init_sock,
  793. .setsockopt = dccp_setsockopt,
  794. .getsockopt = dccp_getsockopt,
  795. .sendmsg = dccp_sendmsg,
  796. .recvmsg = dccp_recvmsg,
  797. .backlog_rcv = dccp_v4_do_rcv,
  798. .hash = dccp_hash,
  799. .unhash = dccp_unhash,
  800. .accept = inet_csk_accept,
  801. .get_port = dccp_v4_get_port,
  802. .shutdown = dccp_shutdown,
  803. .destroy = dccp_destroy_sock,
  804. .orphan_count = &dccp_orphan_count,
  805. .max_header = MAX_DCCP_HEADER,
  806. .obj_size = sizeof(struct dccp_sock),
  807. .rsk_prot = &dccp_request_sock_ops,
  808. .twsk_prot = &dccp_timewait_sock_ops,
  809. #ifdef CONFIG_COMPAT
  810. .compat_setsockopt = compat_dccp_setsockopt,
  811. .compat_getsockopt = compat_dccp_getsockopt,
  812. #endif
  813. };
  814. static struct net_protocol dccp_v4_protocol = {
  815. .handler = dccp_v4_rcv,
  816. .err_handler = dccp_v4_err,
  817. .no_policy = 1,
  818. };
  819. static const struct proto_ops inet_dccp_ops = {
  820. .family = PF_INET,
  821. .owner = THIS_MODULE,
  822. .release = inet_release,
  823. .bind = inet_bind,
  824. .connect = inet_stream_connect,
  825. .socketpair = sock_no_socketpair,
  826. .accept = inet_accept,
  827. .getname = inet_getname,
  828. /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
  829. .poll = dccp_poll,
  830. .ioctl = inet_ioctl,
  831. /* FIXME: work on inet_listen to rename it to sock_common_listen */
  832. .listen = inet_dccp_listen,
  833. .shutdown = inet_shutdown,
  834. .setsockopt = sock_common_setsockopt,
  835. .getsockopt = sock_common_getsockopt,
  836. .sendmsg = inet_sendmsg,
  837. .recvmsg = sock_common_recvmsg,
  838. .mmap = sock_no_mmap,
  839. .sendpage = sock_no_sendpage,
  840. #ifdef CONFIG_COMPAT
  841. .compat_setsockopt = compat_sock_common_setsockopt,
  842. .compat_getsockopt = compat_sock_common_getsockopt,
  843. #endif
  844. };
  845. static struct inet_protosw dccp_v4_protosw = {
  846. .type = SOCK_DCCP,
  847. .protocol = IPPROTO_DCCP,
  848. .prot = &dccp_v4_prot,
  849. .ops = &inet_dccp_ops,
  850. .capability = -1,
  851. .no_check = 0,
  852. .flags = INET_PROTOSW_ICSK,
  853. };
  854. static int __init dccp_v4_init(void)
  855. {
  856. int err = proto_register(&dccp_v4_prot, 1);
  857. if (err != 0)
  858. goto out;
  859. err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
  860. if (err != 0)
  861. goto out_proto_unregister;
  862. inet_register_protosw(&dccp_v4_protosw);
  863. err = inet_csk_ctl_sock_create(&dccp_v4_ctl_socket, PF_INET,
  864. SOCK_DCCP, IPPROTO_DCCP);
  865. if (err)
  866. goto out_unregister_protosw;
  867. out:
  868. return err;
  869. out_unregister_protosw:
  870. inet_unregister_protosw(&dccp_v4_protosw);
  871. inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
  872. out_proto_unregister:
  873. proto_unregister(&dccp_v4_prot);
  874. goto out;
  875. }
  876. static void __exit dccp_v4_exit(void)
  877. {
  878. inet_unregister_protosw(&dccp_v4_protosw);
  879. inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
  880. proto_unregister(&dccp_v4_prot);
  881. }
  882. module_init(dccp_v4_init);
  883. module_exit(dccp_v4_exit);
  884. /*
  885. * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
  886. * values directly, Also cover the case where the protocol is not specified,
  887. * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
  888. */
  889. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
  890. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
  891. MODULE_LICENSE("GPL");
  892. MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
  893. MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");