ipv4.c 29 KB

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