ipv4.c 28 KB

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