ipv4.c 28 KB

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