ipv4.c 27 KB

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