ipv4.c 28 KB

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