ipv4.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 = prandom_u32();
  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;
  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. /* Only need dccph_dport & dccph_sport which are the first
  218. * 4 bytes in dccp header.
  219. * Our caller (icmp_socket_deliver()) already pulled 8 bytes for us.
  220. */
  221. BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_sport) > 8);
  222. BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_dport) > 8);
  223. dh = (struct dccp_hdr *)(skb->data + offset);
  224. sk = __inet_lookup_established(net, &dccp_hashinfo,
  225. iph->daddr, dh->dccph_dport,
  226. iph->saddr, ntohs(dh->dccph_sport),
  227. inet_iif(skb), 0);
  228. if (!sk) {
  229. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  230. return;
  231. }
  232. if (sk->sk_state == DCCP_TIME_WAIT) {
  233. inet_twsk_put(inet_twsk(sk));
  234. return;
  235. }
  236. seq = dccp_hdr_seq(dh);
  237. if (sk->sk_state == DCCP_NEW_SYN_RECV)
  238. return dccp_req_err(sk, seq);
  239. bh_lock_sock(sk);
  240. /* If too many ICMPs get dropped on busy
  241. * servers this needs to be solved differently.
  242. */
  243. if (sock_owned_by_user(sk))
  244. __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
  245. if (sk->sk_state == DCCP_CLOSED)
  246. goto out;
  247. dp = dccp_sk(sk);
  248. if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
  249. !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
  250. __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
  251. goto out;
  252. }
  253. switch (type) {
  254. case ICMP_REDIRECT:
  255. if (!sock_owned_by_user(sk))
  256. dccp_do_redirect(skb, sk);
  257. goto out;
  258. case ICMP_SOURCE_QUENCH:
  259. /* Just silently ignore these. */
  260. goto out;
  261. case ICMP_PARAMETERPROB:
  262. err = EPROTO;
  263. break;
  264. case ICMP_DEST_UNREACH:
  265. if (code > NR_ICMP_UNREACH)
  266. goto out;
  267. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  268. if (!sock_owned_by_user(sk))
  269. dccp_do_pmtu_discovery(sk, iph, info);
  270. goto out;
  271. }
  272. err = icmp_err_convert[code].errno;
  273. break;
  274. case ICMP_TIME_EXCEEDED:
  275. err = EHOSTUNREACH;
  276. break;
  277. default:
  278. goto out;
  279. }
  280. switch (sk->sk_state) {
  281. case DCCP_REQUESTING:
  282. case DCCP_RESPOND:
  283. if (!sock_owned_by_user(sk)) {
  284. __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
  285. sk->sk_err = err;
  286. sk->sk_error_report(sk);
  287. dccp_done(sk);
  288. } else
  289. sk->sk_err_soft = err;
  290. goto out;
  291. }
  292. /* If we've already connected we will keep trying
  293. * until we time out, or the user gives up.
  294. *
  295. * rfc1122 4.2.3.9 allows to consider as hard errors
  296. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  297. * but it is obsoleted by pmtu discovery).
  298. *
  299. * Note, that in modern internet, where routing is unreliable
  300. * and in each dark corner broken firewalls sit, sending random
  301. * errors ordered by their masters even this two messages finally lose
  302. * their original sense (even Linux sends invalid PORT_UNREACHs)
  303. *
  304. * Now we are in compliance with RFCs.
  305. * --ANK (980905)
  306. */
  307. inet = inet_sk(sk);
  308. if (!sock_owned_by_user(sk) && inet->recverr) {
  309. sk->sk_err = err;
  310. sk->sk_error_report(sk);
  311. } else /* Only an error on timeout */
  312. sk->sk_err_soft = err;
  313. out:
  314. bh_unlock_sock(sk);
  315. sock_put(sk);
  316. }
  317. static inline __sum16 dccp_v4_csum_finish(struct sk_buff *skb,
  318. __be32 src, __be32 dst)
  319. {
  320. return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
  321. }
  322. void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb)
  323. {
  324. const struct inet_sock *inet = inet_sk(sk);
  325. struct dccp_hdr *dh = dccp_hdr(skb);
  326. dccp_csum_outgoing(skb);
  327. dh->dccph_checksum = dccp_v4_csum_finish(skb,
  328. inet->inet_saddr,
  329. inet->inet_daddr);
  330. }
  331. EXPORT_SYMBOL_GPL(dccp_v4_send_check);
  332. static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
  333. {
  334. return secure_dccp_sequence_number(ip_hdr(skb)->daddr,
  335. ip_hdr(skb)->saddr,
  336. dccp_hdr(skb)->dccph_dport,
  337. dccp_hdr(skb)->dccph_sport);
  338. }
  339. /*
  340. * The three way handshake has completed - we got a valid ACK or DATAACK -
  341. * now create the new socket.
  342. *
  343. * This is the equivalent of TCP's tcp_v4_syn_recv_sock
  344. */
  345. struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
  346. struct sk_buff *skb,
  347. struct request_sock *req,
  348. struct dst_entry *dst,
  349. struct request_sock *req_unhash,
  350. bool *own_req)
  351. {
  352. struct inet_request_sock *ireq;
  353. struct inet_sock *newinet;
  354. struct sock *newsk;
  355. if (sk_acceptq_is_full(sk))
  356. goto exit_overflow;
  357. newsk = dccp_create_openreq_child(sk, req, skb);
  358. if (newsk == NULL)
  359. goto exit_nonewsk;
  360. newinet = inet_sk(newsk);
  361. ireq = inet_rsk(req);
  362. sk_daddr_set(newsk, ireq->ir_rmt_addr);
  363. sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
  364. newinet->inet_saddr = ireq->ir_loc_addr;
  365. RCU_INIT_POINTER(newinet->inet_opt, rcu_dereference(ireq->ireq_opt));
  366. newinet->mc_index = inet_iif(skb);
  367. newinet->mc_ttl = ip_hdr(skb)->ttl;
  368. newinet->inet_id = prandom_u32();
  369. if (dst == NULL && (dst = inet_csk_route_child_sock(sk, newsk, req)) == NULL)
  370. goto put_and_exit;
  371. sk_setup_caps(newsk, dst);
  372. dccp_sync_mss(newsk, dst_mtu(dst));
  373. if (__inet_inherit_port(sk, newsk) < 0)
  374. goto put_and_exit;
  375. *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
  376. if (*own_req)
  377. ireq->ireq_opt = NULL;
  378. else
  379. newinet->inet_opt = NULL;
  380. return newsk;
  381. exit_overflow:
  382. __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  383. exit_nonewsk:
  384. dst_release(dst);
  385. exit:
  386. __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
  387. return NULL;
  388. put_and_exit:
  389. newinet->inet_opt = NULL;
  390. inet_csk_prepare_forced_close(newsk);
  391. dccp_done(newsk);
  392. goto exit;
  393. }
  394. EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
  395. static struct dst_entry* dccp_v4_route_skb(struct net *net, struct sock *sk,
  396. struct sk_buff *skb)
  397. {
  398. struct rtable *rt;
  399. const struct iphdr *iph = ip_hdr(skb);
  400. struct flowi4 fl4 = {
  401. .flowi4_oif = inet_iif(skb),
  402. .daddr = iph->saddr,
  403. .saddr = iph->daddr,
  404. .flowi4_tos = RT_CONN_FLAGS(sk),
  405. .flowi4_proto = sk->sk_protocol,
  406. .fl4_sport = dccp_hdr(skb)->dccph_dport,
  407. .fl4_dport = dccp_hdr(skb)->dccph_sport,
  408. };
  409. security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
  410. rt = ip_route_output_flow(net, &fl4, sk);
  411. if (IS_ERR(rt)) {
  412. IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
  413. return NULL;
  414. }
  415. return &rt->dst;
  416. }
  417. static int dccp_v4_send_response(const struct sock *sk, struct request_sock *req)
  418. {
  419. int err = -1;
  420. struct sk_buff *skb;
  421. struct dst_entry *dst;
  422. struct flowi4 fl4;
  423. dst = inet_csk_route_req(sk, &fl4, req);
  424. if (dst == NULL)
  425. goto out;
  426. skb = dccp_make_response(sk, dst, req);
  427. if (skb != NULL) {
  428. const struct inet_request_sock *ireq = inet_rsk(req);
  429. struct dccp_hdr *dh = dccp_hdr(skb);
  430. dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->ir_loc_addr,
  431. ireq->ir_rmt_addr);
  432. rcu_read_lock();
  433. err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
  434. ireq->ir_rmt_addr,
  435. rcu_dereference(ireq->ireq_opt));
  436. rcu_read_unlock();
  437. err = net_xmit_eval(err);
  438. }
  439. out:
  440. dst_release(dst);
  441. return err;
  442. }
  443. static void dccp_v4_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
  444. {
  445. int err;
  446. const struct iphdr *rxiph;
  447. struct sk_buff *skb;
  448. struct dst_entry *dst;
  449. struct net *net = dev_net(skb_dst(rxskb)->dev);
  450. struct sock *ctl_sk = net->dccp.v4_ctl_sk;
  451. /* Never send a reset in response to a reset. */
  452. if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
  453. return;
  454. if (skb_rtable(rxskb)->rt_type != RTN_LOCAL)
  455. return;
  456. dst = dccp_v4_route_skb(net, ctl_sk, rxskb);
  457. if (dst == NULL)
  458. return;
  459. skb = dccp_ctl_make_reset(ctl_sk, rxskb);
  460. if (skb == NULL)
  461. goto out;
  462. rxiph = ip_hdr(rxskb);
  463. dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
  464. rxiph->daddr);
  465. skb_dst_set(skb, dst_clone(dst));
  466. local_bh_disable();
  467. bh_lock_sock(ctl_sk);
  468. err = ip_build_and_send_pkt(skb, ctl_sk,
  469. rxiph->daddr, rxiph->saddr, NULL);
  470. bh_unlock_sock(ctl_sk);
  471. if (net_xmit_eval(err) == 0) {
  472. __DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  473. __DCCP_INC_STATS(DCCP_MIB_OUTRSTS);
  474. }
  475. local_bh_enable();
  476. out:
  477. dst_release(dst);
  478. }
  479. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  480. {
  481. dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
  482. kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
  483. }
  484. void dccp_syn_ack_timeout(const struct request_sock *req)
  485. {
  486. }
  487. EXPORT_SYMBOL(dccp_syn_ack_timeout);
  488. static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
  489. .family = PF_INET,
  490. .obj_size = sizeof(struct dccp_request_sock),
  491. .rtx_syn_ack = dccp_v4_send_response,
  492. .send_ack = dccp_reqsk_send_ack,
  493. .destructor = dccp_v4_reqsk_destructor,
  494. .send_reset = dccp_v4_ctl_send_reset,
  495. .syn_ack_timeout = dccp_syn_ack_timeout,
  496. };
  497. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  498. {
  499. struct inet_request_sock *ireq;
  500. struct request_sock *req;
  501. struct dccp_request_sock *dreq;
  502. const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
  503. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  504. /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
  505. if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  506. return 0; /* discard, don't send a reset here */
  507. if (dccp_bad_service_code(sk, service)) {
  508. dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
  509. goto drop;
  510. }
  511. /*
  512. * TW buckets are converted to open requests without
  513. * limitations, they conserve resources and peer is
  514. * evidently real one.
  515. */
  516. dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  517. if (inet_csk_reqsk_queue_is_full(sk))
  518. goto drop;
  519. if (sk_acceptq_is_full(sk))
  520. goto drop;
  521. req = inet_reqsk_alloc(&dccp_request_sock_ops, sk, true);
  522. if (req == NULL)
  523. goto drop;
  524. if (dccp_reqsk_init(req, dccp_sk(sk), skb))
  525. goto drop_and_free;
  526. dreq = dccp_rsk(req);
  527. if (dccp_parse_options(sk, dreq, skb))
  528. goto drop_and_free;
  529. if (security_inet_conn_request(sk, skb, req))
  530. goto drop_and_free;
  531. ireq = inet_rsk(req);
  532. sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
  533. sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
  534. ireq->ir_mark = inet_request_mark(sk, skb);
  535. ireq->ireq_family = AF_INET;
  536. ireq->ir_iif = sk->sk_bound_dev_if;
  537. /*
  538. * Step 3: Process LISTEN state
  539. *
  540. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  541. *
  542. * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
  543. */
  544. dreq->dreq_isr = dcb->dccpd_seq;
  545. dreq->dreq_gsr = dreq->dreq_isr;
  546. dreq->dreq_iss = dccp_v4_init_sequence(skb);
  547. dreq->dreq_gss = dreq->dreq_iss;
  548. dreq->dreq_service = service;
  549. if (dccp_v4_send_response(sk, req))
  550. goto drop_and_free;
  551. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  552. reqsk_put(req);
  553. return 0;
  554. drop_and_free:
  555. reqsk_free(req);
  556. drop:
  557. __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
  558. return -1;
  559. }
  560. EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
  561. int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  562. {
  563. struct dccp_hdr *dh = dccp_hdr(skb);
  564. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  565. if (dccp_rcv_established(sk, skb, dh, skb->len))
  566. goto reset;
  567. return 0;
  568. }
  569. /*
  570. * Step 3: Process LISTEN state
  571. * If P.type == Request or P contains a valid Init Cookie option,
  572. * (* Must scan the packet's options to check for Init
  573. * Cookies. Only Init Cookies are processed here,
  574. * however; other options are processed in Step 8. This
  575. * scan need only be performed if the endpoint uses Init
  576. * Cookies *)
  577. * (* Generate a new socket and switch to that socket *)
  578. * Set S := new socket for this port pair
  579. * S.state = RESPOND
  580. * Choose S.ISS (initial seqno) or set from Init Cookies
  581. * Initialize S.GAR := S.ISS
  582. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
  583. * Continue with S.state == RESPOND
  584. * (* A Response packet will be generated in Step 11 *)
  585. * Otherwise,
  586. * Generate Reset(No Connection) unless P.type == Reset
  587. * Drop packet and return
  588. *
  589. * NOTE: the check for the packet types is done in
  590. * dccp_rcv_state_process
  591. */
  592. if (dccp_rcv_state_process(sk, skb, dh, skb->len))
  593. goto reset;
  594. return 0;
  595. reset:
  596. dccp_v4_ctl_send_reset(sk, skb);
  597. kfree_skb(skb);
  598. return 0;
  599. }
  600. EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
  601. /**
  602. * dccp_invalid_packet - check for malformed packets
  603. * Implements RFC 4340, 8.5: Step 1: Check header basics
  604. * Packets that fail these checks are ignored and do not receive Resets.
  605. */
  606. int dccp_invalid_packet(struct sk_buff *skb)
  607. {
  608. const struct dccp_hdr *dh;
  609. unsigned int cscov;
  610. u8 dccph_doff;
  611. if (skb->pkt_type != PACKET_HOST)
  612. return 1;
  613. /* If the packet is shorter than 12 bytes, drop packet and return */
  614. if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
  615. DCCP_WARN("pskb_may_pull failed\n");
  616. return 1;
  617. }
  618. dh = dccp_hdr(skb);
  619. /* If P.type is not understood, drop packet and return */
  620. if (dh->dccph_type >= DCCP_PKT_INVALID) {
  621. DCCP_WARN("invalid packet type\n");
  622. return 1;
  623. }
  624. /*
  625. * If P.Data Offset is too small for packet type, drop packet and return
  626. */
  627. dccph_doff = dh->dccph_doff;
  628. if (dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
  629. DCCP_WARN("P.Data Offset(%u) too small\n", dccph_doff);
  630. return 1;
  631. }
  632. /*
  633. * If P.Data Offset is too too large for packet, drop packet and return
  634. */
  635. if (!pskb_may_pull(skb, dccph_doff * sizeof(u32))) {
  636. DCCP_WARN("P.Data Offset(%u) too large\n", dccph_doff);
  637. return 1;
  638. }
  639. dh = dccp_hdr(skb);
  640. /*
  641. * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
  642. * has short sequence numbers), drop packet and return
  643. */
  644. if ((dh->dccph_type < DCCP_PKT_DATA ||
  645. dh->dccph_type > DCCP_PKT_DATAACK) && dh->dccph_x == 0) {
  646. DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
  647. dccp_packet_name(dh->dccph_type));
  648. return 1;
  649. }
  650. /*
  651. * If P.CsCov is too large for the packet size, drop packet and return.
  652. * This must come _before_ checksumming (not as RFC 4340 suggests).
  653. */
  654. cscov = dccp_csum_coverage(skb);
  655. if (cscov > skb->len) {
  656. DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
  657. dh->dccph_cscov, skb->len);
  658. return 1;
  659. }
  660. /* If header checksum is incorrect, drop packet and return.
  661. * (This step is completed in the AF-dependent functions.) */
  662. skb->csum = skb_checksum(skb, 0, cscov, 0);
  663. return 0;
  664. }
  665. EXPORT_SYMBOL_GPL(dccp_invalid_packet);
  666. /* this is called when real data arrives */
  667. static int dccp_v4_rcv(struct sk_buff *skb)
  668. {
  669. const struct dccp_hdr *dh;
  670. const struct iphdr *iph;
  671. bool refcounted;
  672. struct sock *sk;
  673. int min_cov;
  674. /* Step 1: Check header basics */
  675. if (dccp_invalid_packet(skb))
  676. goto discard_it;
  677. iph = ip_hdr(skb);
  678. /* Step 1: If header checksum is incorrect, drop packet and return */
  679. if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
  680. DCCP_WARN("dropped packet with invalid checksum\n");
  681. goto discard_it;
  682. }
  683. dh = dccp_hdr(skb);
  684. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
  685. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  686. dccp_pr_debug("%8.8s src=%pI4@%-5d dst=%pI4@%-5d seq=%llu",
  687. dccp_packet_name(dh->dccph_type),
  688. &iph->saddr, ntohs(dh->dccph_sport),
  689. &iph->daddr, ntohs(dh->dccph_dport),
  690. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
  691. if (dccp_packet_without_ack(skb)) {
  692. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  693. dccp_pr_debug_cat("\n");
  694. } else {
  695. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  696. dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
  697. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  698. }
  699. lookup:
  700. sk = __inet_lookup_skb(&dccp_hashinfo, skb, __dccp_hdr_len(dh),
  701. dh->dccph_sport, dh->dccph_dport, 0, &refcounted);
  702. if (!sk) {
  703. dccp_pr_debug("failed to look up flow ID in table and "
  704. "get corresponding socket\n");
  705. goto no_dccp_socket;
  706. }
  707. /*
  708. * Step 2:
  709. * ... or S.state == TIMEWAIT,
  710. * Generate Reset(No Connection) unless P.type == Reset
  711. * Drop packet and return
  712. */
  713. if (sk->sk_state == DCCP_TIME_WAIT) {
  714. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
  715. inet_twsk_put(inet_twsk(sk));
  716. goto no_dccp_socket;
  717. }
  718. if (sk->sk_state == DCCP_NEW_SYN_RECV) {
  719. struct request_sock *req = inet_reqsk(sk);
  720. struct sock *nsk;
  721. sk = req->rsk_listener;
  722. if (unlikely(sk->sk_state != DCCP_LISTEN)) {
  723. inet_csk_reqsk_queue_drop_and_put(sk, req);
  724. goto lookup;
  725. }
  726. sock_hold(sk);
  727. refcounted = true;
  728. nsk = dccp_check_req(sk, skb, req);
  729. if (!nsk) {
  730. reqsk_put(req);
  731. goto discard_and_relse;
  732. }
  733. if (nsk == sk) {
  734. reqsk_put(req);
  735. } else if (dccp_child_process(sk, nsk, skb)) {
  736. dccp_v4_ctl_send_reset(sk, skb);
  737. goto discard_and_relse;
  738. } else {
  739. sock_put(sk);
  740. return 0;
  741. }
  742. }
  743. /*
  744. * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
  745. * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
  746. * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
  747. */
  748. min_cov = dccp_sk(sk)->dccps_pcrlen;
  749. if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
  750. dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
  751. dh->dccph_cscov, min_cov);
  752. /* FIXME: "Such packets SHOULD be reported using Data Dropped
  753. * options (Section 11.7) with Drop Code 0, Protocol
  754. * Constraints." */
  755. goto discard_and_relse;
  756. }
  757. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  758. goto discard_and_relse;
  759. nf_reset(skb);
  760. return __sk_receive_skb(sk, skb, 1, dh->dccph_doff * 4, refcounted);
  761. no_dccp_socket:
  762. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  763. goto discard_it;
  764. /*
  765. * Step 2:
  766. * If no socket ...
  767. * Generate Reset(No Connection) unless P.type == Reset
  768. * Drop packet and return
  769. */
  770. if (dh->dccph_type != DCCP_PKT_RESET) {
  771. DCCP_SKB_CB(skb)->dccpd_reset_code =
  772. DCCP_RESET_CODE_NO_CONNECTION;
  773. dccp_v4_ctl_send_reset(sk, skb);
  774. }
  775. discard_it:
  776. kfree_skb(skb);
  777. return 0;
  778. discard_and_relse:
  779. if (refcounted)
  780. sock_put(sk);
  781. goto discard_it;
  782. }
  783. static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
  784. .queue_xmit = ip_queue_xmit,
  785. .send_check = dccp_v4_send_check,
  786. .rebuild_header = inet_sk_rebuild_header,
  787. .conn_request = dccp_v4_conn_request,
  788. .syn_recv_sock = dccp_v4_request_recv_sock,
  789. .net_header_len = sizeof(struct iphdr),
  790. .setsockopt = ip_setsockopt,
  791. .getsockopt = ip_getsockopt,
  792. .addr2sockaddr = inet_csk_addr2sockaddr,
  793. .sockaddr_len = sizeof(struct sockaddr_in),
  794. #ifdef CONFIG_COMPAT
  795. .compat_setsockopt = compat_ip_setsockopt,
  796. .compat_getsockopt = compat_ip_getsockopt,
  797. #endif
  798. };
  799. static int dccp_v4_init_sock(struct sock *sk)
  800. {
  801. static __u8 dccp_v4_ctl_sock_initialized;
  802. int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
  803. if (err == 0) {
  804. if (unlikely(!dccp_v4_ctl_sock_initialized))
  805. dccp_v4_ctl_sock_initialized = 1;
  806. inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
  807. }
  808. return err;
  809. }
  810. static struct timewait_sock_ops dccp_timewait_sock_ops = {
  811. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  812. };
  813. static struct proto dccp_v4_prot = {
  814. .name = "DCCP",
  815. .owner = THIS_MODULE,
  816. .close = dccp_close,
  817. .connect = dccp_v4_connect,
  818. .disconnect = dccp_disconnect,
  819. .ioctl = dccp_ioctl,
  820. .init = dccp_v4_init_sock,
  821. .setsockopt = dccp_setsockopt,
  822. .getsockopt = dccp_getsockopt,
  823. .sendmsg = dccp_sendmsg,
  824. .recvmsg = dccp_recvmsg,
  825. .backlog_rcv = dccp_v4_do_rcv,
  826. .hash = inet_hash,
  827. .unhash = inet_unhash,
  828. .accept = inet_csk_accept,
  829. .get_port = inet_csk_get_port,
  830. .shutdown = dccp_shutdown,
  831. .destroy = dccp_destroy_sock,
  832. .orphan_count = &dccp_orphan_count,
  833. .max_header = MAX_DCCP_HEADER,
  834. .obj_size = sizeof(struct dccp_sock),
  835. .slab_flags = SLAB_TYPESAFE_BY_RCU,
  836. .rsk_prot = &dccp_request_sock_ops,
  837. .twsk_prot = &dccp_timewait_sock_ops,
  838. .h.hashinfo = &dccp_hashinfo,
  839. #ifdef CONFIG_COMPAT
  840. .compat_setsockopt = compat_dccp_setsockopt,
  841. .compat_getsockopt = compat_dccp_getsockopt,
  842. #endif
  843. };
  844. static const struct net_protocol dccp_v4_protocol = {
  845. .handler = dccp_v4_rcv,
  846. .err_handler = dccp_v4_err,
  847. .no_policy = 1,
  848. .netns_ok = 1,
  849. .icmp_strict_tag_validation = 1,
  850. };
  851. static const struct proto_ops inet_dccp_ops = {
  852. .family = PF_INET,
  853. .owner = THIS_MODULE,
  854. .release = inet_release,
  855. .bind = inet_bind,
  856. .connect = inet_stream_connect,
  857. .socketpair = sock_no_socketpair,
  858. .accept = inet_accept,
  859. .getname = inet_getname,
  860. /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
  861. .poll = dccp_poll,
  862. .ioctl = inet_ioctl,
  863. /* FIXME: work on inet_listen to rename it to sock_common_listen */
  864. .listen = inet_dccp_listen,
  865. .shutdown = inet_shutdown,
  866. .setsockopt = sock_common_setsockopt,
  867. .getsockopt = sock_common_getsockopt,
  868. .sendmsg = inet_sendmsg,
  869. .recvmsg = sock_common_recvmsg,
  870. .mmap = sock_no_mmap,
  871. .sendpage = sock_no_sendpage,
  872. #ifdef CONFIG_COMPAT
  873. .compat_setsockopt = compat_sock_common_setsockopt,
  874. .compat_getsockopt = compat_sock_common_getsockopt,
  875. #endif
  876. };
  877. static struct inet_protosw dccp_v4_protosw = {
  878. .type = SOCK_DCCP,
  879. .protocol = IPPROTO_DCCP,
  880. .prot = &dccp_v4_prot,
  881. .ops = &inet_dccp_ops,
  882. .flags = INET_PROTOSW_ICSK,
  883. };
  884. static int __net_init dccp_v4_init_net(struct net *net)
  885. {
  886. if (dccp_hashinfo.bhash == NULL)
  887. return -ESOCKTNOSUPPORT;
  888. return inet_ctl_sock_create(&net->dccp.v4_ctl_sk, PF_INET,
  889. SOCK_DCCP, IPPROTO_DCCP, net);
  890. }
  891. static void __net_exit dccp_v4_exit_net(struct net *net)
  892. {
  893. inet_ctl_sock_destroy(net->dccp.v4_ctl_sk);
  894. }
  895. static void __net_exit dccp_v4_exit_batch(struct list_head *net_exit_list)
  896. {
  897. inet_twsk_purge(&dccp_hashinfo, AF_INET);
  898. }
  899. static struct pernet_operations dccp_v4_ops = {
  900. .init = dccp_v4_init_net,
  901. .exit = dccp_v4_exit_net,
  902. .exit_batch = dccp_v4_exit_batch,
  903. };
  904. static int __init dccp_v4_init(void)
  905. {
  906. int err = proto_register(&dccp_v4_prot, 1);
  907. if (err)
  908. goto out;
  909. inet_register_protosw(&dccp_v4_protosw);
  910. err = register_pernet_subsys(&dccp_v4_ops);
  911. if (err)
  912. goto out_destroy_ctl_sock;
  913. err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
  914. if (err)
  915. goto out_proto_unregister;
  916. out:
  917. return err;
  918. out_proto_unregister:
  919. unregister_pernet_subsys(&dccp_v4_ops);
  920. out_destroy_ctl_sock:
  921. inet_unregister_protosw(&dccp_v4_protosw);
  922. proto_unregister(&dccp_v4_prot);
  923. goto out;
  924. }
  925. static void __exit dccp_v4_exit(void)
  926. {
  927. inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
  928. unregister_pernet_subsys(&dccp_v4_ops);
  929. inet_unregister_protosw(&dccp_v4_protosw);
  930. proto_unregister(&dccp_v4_prot);
  931. }
  932. module_init(dccp_v4_init);
  933. module_exit(dccp_v4_exit);
  934. /*
  935. * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
  936. * values directly, Also cover the case where the protocol is not specified,
  937. * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
  938. */
  939. /* MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 33, 6); */
  940. /* MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 0, 6); */
  941. MODULE_LICENSE("GPL");
  942. MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
  943. MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");