ipv4.c 27 KB

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