ipv4.c 28 KB

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