ipv4.c 28 KB

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