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);
  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. {
  413. int err = -1;
  414. struct sk_buff *skb;
  415. struct dst_entry *dst;
  416. dst = inet_csk_route_req(sk, req);
  417. if (dst == NULL)
  418. goto out;
  419. skb = dccp_make_response(sk, dst, req);
  420. if (skb != NULL) {
  421. const struct inet_request_sock *ireq = inet_rsk(req);
  422. struct dccp_hdr *dh = dccp_hdr(skb);
  423. dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->loc_addr,
  424. ireq->rmt_addr);
  425. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  426. ireq->rmt_addr,
  427. ireq->opt);
  428. err = net_xmit_eval(err);
  429. }
  430. out:
  431. dst_release(dst);
  432. return err;
  433. }
  434. static void dccp_v4_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb)
  435. {
  436. int err;
  437. const struct iphdr *rxiph;
  438. struct sk_buff *skb;
  439. struct dst_entry *dst;
  440. struct net *net = dev_net(skb_dst(rxskb)->dev);
  441. struct sock *ctl_sk = net->dccp.v4_ctl_sk;
  442. /* Never send a reset in response to a reset. */
  443. if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
  444. return;
  445. if (skb_rtable(rxskb)->rt_type != RTN_LOCAL)
  446. return;
  447. dst = dccp_v4_route_skb(net, ctl_sk, rxskb);
  448. if (dst == NULL)
  449. return;
  450. skb = dccp_ctl_make_reset(ctl_sk, rxskb);
  451. if (skb == NULL)
  452. goto out;
  453. rxiph = ip_hdr(rxskb);
  454. dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
  455. rxiph->daddr);
  456. skb_dst_set(skb, dst_clone(dst));
  457. bh_lock_sock(ctl_sk);
  458. err = ip_build_and_send_pkt(skb, ctl_sk,
  459. rxiph->daddr, rxiph->saddr, NULL);
  460. bh_unlock_sock(ctl_sk);
  461. if (net_xmit_eval(err) == 0) {
  462. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  463. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  464. }
  465. out:
  466. dst_release(dst);
  467. }
  468. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  469. {
  470. dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
  471. kfree(inet_rsk(req)->opt);
  472. }
  473. static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
  474. .family = PF_INET,
  475. .obj_size = sizeof(struct dccp_request_sock),
  476. .rtx_syn_ack = dccp_v4_send_response,
  477. .send_ack = dccp_reqsk_send_ack,
  478. .destructor = dccp_v4_reqsk_destructor,
  479. .send_reset = dccp_v4_ctl_send_reset,
  480. };
  481. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  482. {
  483. struct inet_request_sock *ireq;
  484. struct request_sock *req;
  485. struct dccp_request_sock *dreq;
  486. const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
  487. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  488. /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
  489. if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  490. return 0; /* discard, don't send a reset here */
  491. if (dccp_bad_service_code(sk, service)) {
  492. dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
  493. goto drop;
  494. }
  495. /*
  496. * TW buckets are converted to open requests without
  497. * limitations, they conserve resources and peer is
  498. * evidently real one.
  499. */
  500. dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  501. if (inet_csk_reqsk_queue_is_full(sk))
  502. goto drop;
  503. /*
  504. * Accept backlog is full. If we have already queued enough
  505. * of warm entries in syn queue, drop request. It is better than
  506. * clogging syn queue with openreqs with exponentially increasing
  507. * timeout.
  508. */
  509. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  510. goto drop;
  511. req = inet_reqsk_alloc(&dccp_request_sock_ops);
  512. if (req == NULL)
  513. goto drop;
  514. if (dccp_reqsk_init(req, dccp_sk(sk), skb))
  515. goto drop_and_free;
  516. dreq = dccp_rsk(req);
  517. if (dccp_parse_options(sk, dreq, skb))
  518. goto drop_and_free;
  519. if (security_inet_conn_request(sk, skb, req))
  520. goto drop_and_free;
  521. ireq = inet_rsk(req);
  522. ireq->loc_addr = ip_hdr(skb)->daddr;
  523. ireq->rmt_addr = ip_hdr(skb)->saddr;
  524. /*
  525. * Step 3: Process LISTEN state
  526. *
  527. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  528. *
  529. * In fact we defer setting S.GSR, S.SWL, S.SWH to
  530. * dccp_create_openreq_child.
  531. */
  532. dreq->dreq_isr = dcb->dccpd_seq;
  533. dreq->dreq_iss = dccp_v4_init_sequence(skb);
  534. dreq->dreq_service = service;
  535. if (dccp_v4_send_response(sk, req))
  536. goto drop_and_free;
  537. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  538. return 0;
  539. drop_and_free:
  540. reqsk_free(req);
  541. drop:
  542. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  543. return -1;
  544. }
  545. EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
  546. int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  547. {
  548. struct dccp_hdr *dh = dccp_hdr(skb);
  549. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  550. if (dccp_rcv_established(sk, skb, dh, skb->len))
  551. goto reset;
  552. return 0;
  553. }
  554. /*
  555. * Step 3: Process LISTEN state
  556. * If P.type == Request or P contains a valid Init Cookie option,
  557. * (* Must scan the packet's options to check for Init
  558. * Cookies. Only Init Cookies are processed here,
  559. * however; other options are processed in Step 8. This
  560. * scan need only be performed if the endpoint uses Init
  561. * Cookies *)
  562. * (* Generate a new socket and switch to that socket *)
  563. * Set S := new socket for this port pair
  564. * S.state = RESPOND
  565. * Choose S.ISS (initial seqno) or set from Init Cookies
  566. * Initialize S.GAR := S.ISS
  567. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
  568. * Continue with S.state == RESPOND
  569. * (* A Response packet will be generated in Step 11 *)
  570. * Otherwise,
  571. * Generate Reset(No Connection) unless P.type == Reset
  572. * Drop packet and return
  573. *
  574. * NOTE: the check for the packet types is done in
  575. * dccp_rcv_state_process
  576. */
  577. if (sk->sk_state == DCCP_LISTEN) {
  578. struct sock *nsk = dccp_v4_hnd_req(sk, skb);
  579. if (nsk == NULL)
  580. goto discard;
  581. if (nsk != sk) {
  582. if (dccp_child_process(sk, nsk, skb))
  583. goto reset;
  584. return 0;
  585. }
  586. }
  587. if (dccp_rcv_state_process(sk, skb, dh, skb->len))
  588. goto reset;
  589. return 0;
  590. reset:
  591. dccp_v4_ctl_send_reset(sk, skb);
  592. discard:
  593. kfree_skb(skb);
  594. return 0;
  595. }
  596. EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
  597. /**
  598. * dccp_invalid_packet - check for malformed packets
  599. * Implements RFC 4340, 8.5: Step 1: Check header basics
  600. * Packets that fail these checks are ignored and do not receive Resets.
  601. */
  602. int dccp_invalid_packet(struct sk_buff *skb)
  603. {
  604. const struct dccp_hdr *dh;
  605. unsigned int cscov;
  606. if (skb->pkt_type != PACKET_HOST)
  607. return 1;
  608. /* If the packet is shorter than 12 bytes, drop packet and return */
  609. if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
  610. DCCP_WARN("pskb_may_pull failed\n");
  611. return 1;
  612. }
  613. dh = dccp_hdr(skb);
  614. /* If P.type is not understood, drop packet and return */
  615. if (dh->dccph_type >= DCCP_PKT_INVALID) {
  616. DCCP_WARN("invalid packet type\n");
  617. return 1;
  618. }
  619. /*
  620. * If P.Data Offset is too small for packet type, drop packet and return
  621. */
  622. if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
  623. DCCP_WARN("P.Data Offset(%u) too small\n", dh->dccph_doff);
  624. return 1;
  625. }
  626. /*
  627. * If P.Data Offset is too too large for packet, drop packet and return
  628. */
  629. if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
  630. DCCP_WARN("P.Data Offset(%u) too large\n", dh->dccph_doff);
  631. return 1;
  632. }
  633. /*
  634. * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
  635. * has short sequence numbers), drop packet and return
  636. */
  637. if ((dh->dccph_type < DCCP_PKT_DATA ||
  638. dh->dccph_type > DCCP_PKT_DATAACK) && dh->dccph_x == 0) {
  639. DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
  640. dccp_packet_name(dh->dccph_type));
  641. return 1;
  642. }
  643. /*
  644. * If P.CsCov is too large for the packet size, drop packet and return.
  645. * This must come _before_ checksumming (not as RFC 4340 suggests).
  646. */
  647. cscov = dccp_csum_coverage(skb);
  648. if (cscov > skb->len) {
  649. DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
  650. dh->dccph_cscov, skb->len);
  651. return 1;
  652. }
  653. /* If header checksum is incorrect, drop packet and return.
  654. * (This step is completed in the AF-dependent functions.) */
  655. skb->csum = skb_checksum(skb, 0, cscov, 0);
  656. return 0;
  657. }
  658. EXPORT_SYMBOL_GPL(dccp_invalid_packet);
  659. /* this is called when real data arrives */
  660. static int dccp_v4_rcv(struct sk_buff *skb)
  661. {
  662. const struct dccp_hdr *dh;
  663. const struct iphdr *iph;
  664. struct sock *sk;
  665. int min_cov;
  666. /* Step 1: Check header basics */
  667. if (dccp_invalid_packet(skb))
  668. goto discard_it;
  669. iph = ip_hdr(skb);
  670. /* Step 1: If header checksum is incorrect, drop packet and return */
  671. if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
  672. DCCP_WARN("dropped packet with invalid checksum\n");
  673. goto discard_it;
  674. }
  675. dh = dccp_hdr(skb);
  676. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
  677. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  678. dccp_pr_debug("%8.8s src=%pI4@%-5d dst=%pI4@%-5d seq=%llu",
  679. dccp_packet_name(dh->dccph_type),
  680. &iph->saddr, ntohs(dh->dccph_sport),
  681. &iph->daddr, ntohs(dh->dccph_dport),
  682. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
  683. if (dccp_packet_without_ack(skb)) {
  684. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  685. dccp_pr_debug_cat("\n");
  686. } else {
  687. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  688. dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
  689. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  690. }
  691. /* Step 2:
  692. * Look up flow ID in table and get corresponding socket */
  693. sk = __inet_lookup_skb(&dccp_hashinfo, skb,
  694. dh->dccph_sport, dh->dccph_dport);
  695. /*
  696. * Step 2:
  697. * If no socket ...
  698. */
  699. if (sk == NULL) {
  700. dccp_pr_debug("failed to look up flow ID in table and "
  701. "get corresponding socket\n");
  702. goto no_dccp_socket;
  703. }
  704. /*
  705. * Step 2:
  706. * ... or S.state == TIMEWAIT,
  707. * Generate Reset(No Connection) unless P.type == Reset
  708. * Drop packet and return
  709. */
  710. if (sk->sk_state == DCCP_TIME_WAIT) {
  711. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
  712. inet_twsk_put(inet_twsk(sk));
  713. goto no_dccp_socket;
  714. }
  715. /*
  716. * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
  717. * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
  718. * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
  719. */
  720. min_cov = dccp_sk(sk)->dccps_pcrlen;
  721. if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
  722. dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
  723. dh->dccph_cscov, min_cov);
  724. /* FIXME: "Such packets SHOULD be reported using Data Dropped
  725. * options (Section 11.7) with Drop Code 0, Protocol
  726. * Constraints." */
  727. goto discard_and_relse;
  728. }
  729. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  730. goto discard_and_relse;
  731. nf_reset(skb);
  732. return sk_receive_skb(sk, skb, 1);
  733. no_dccp_socket:
  734. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  735. goto discard_it;
  736. /*
  737. * Step 2:
  738. * If no socket ...
  739. * Generate Reset(No Connection) unless P.type == Reset
  740. * Drop packet and return
  741. */
  742. if (dh->dccph_type != DCCP_PKT_RESET) {
  743. DCCP_SKB_CB(skb)->dccpd_reset_code =
  744. DCCP_RESET_CODE_NO_CONNECTION;
  745. dccp_v4_ctl_send_reset(sk, skb);
  746. }
  747. discard_it:
  748. kfree_skb(skb);
  749. return 0;
  750. discard_and_relse:
  751. sock_put(sk);
  752. goto discard_it;
  753. }
  754. static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
  755. .queue_xmit = ip_queue_xmit,
  756. .send_check = dccp_v4_send_check,
  757. .rebuild_header = inet_sk_rebuild_header,
  758. .conn_request = dccp_v4_conn_request,
  759. .syn_recv_sock = dccp_v4_request_recv_sock,
  760. .net_header_len = sizeof(struct iphdr),
  761. .setsockopt = ip_setsockopt,
  762. .getsockopt = ip_getsockopt,
  763. .addr2sockaddr = inet_csk_addr2sockaddr,
  764. .sockaddr_len = sizeof(struct sockaddr_in),
  765. .bind_conflict = inet_csk_bind_conflict,
  766. #ifdef CONFIG_COMPAT
  767. .compat_setsockopt = compat_ip_setsockopt,
  768. .compat_getsockopt = compat_ip_getsockopt,
  769. #endif
  770. };
  771. static int dccp_v4_init_sock(struct sock *sk)
  772. {
  773. static __u8 dccp_v4_ctl_sock_initialized;
  774. int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
  775. if (err == 0) {
  776. if (unlikely(!dccp_v4_ctl_sock_initialized))
  777. dccp_v4_ctl_sock_initialized = 1;
  778. inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
  779. }
  780. return err;
  781. }
  782. static struct timewait_sock_ops dccp_timewait_sock_ops = {
  783. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  784. };
  785. static struct proto dccp_v4_prot = {
  786. .name = "DCCP",
  787. .owner = THIS_MODULE,
  788. .close = dccp_close,
  789. .connect = dccp_v4_connect,
  790. .disconnect = dccp_disconnect,
  791. .ioctl = dccp_ioctl,
  792. .init = dccp_v4_init_sock,
  793. .setsockopt = dccp_setsockopt,
  794. .getsockopt = dccp_getsockopt,
  795. .sendmsg = dccp_sendmsg,
  796. .recvmsg = dccp_recvmsg,
  797. .backlog_rcv = dccp_v4_do_rcv,
  798. .hash = inet_hash,
  799. .unhash = inet_unhash,
  800. .accept = inet_csk_accept,
  801. .get_port = inet_csk_get_port,
  802. .shutdown = dccp_shutdown,
  803. .destroy = dccp_destroy_sock,
  804. .orphan_count = &dccp_orphan_count,
  805. .max_header = MAX_DCCP_HEADER,
  806. .obj_size = sizeof(struct dccp_sock),
  807. .slab_flags = SLAB_DESTROY_BY_RCU,
  808. .rsk_prot = &dccp_request_sock_ops,
  809. .twsk_prot = &dccp_timewait_sock_ops,
  810. .h.hashinfo = &dccp_hashinfo,
  811. #ifdef CONFIG_COMPAT
  812. .compat_setsockopt = compat_dccp_setsockopt,
  813. .compat_getsockopt = compat_dccp_getsockopt,
  814. #endif
  815. };
  816. static const struct net_protocol dccp_v4_protocol = {
  817. .handler = dccp_v4_rcv,
  818. .err_handler = dccp_v4_err,
  819. .no_policy = 1,
  820. .netns_ok = 1,
  821. };
  822. static const struct proto_ops inet_dccp_ops = {
  823. .family = PF_INET,
  824. .owner = THIS_MODULE,
  825. .release = inet_release,
  826. .bind = inet_bind,
  827. .connect = inet_stream_connect,
  828. .socketpair = sock_no_socketpair,
  829. .accept = inet_accept,
  830. .getname = inet_getname,
  831. /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
  832. .poll = dccp_poll,
  833. .ioctl = inet_ioctl,
  834. /* FIXME: work on inet_listen to rename it to sock_common_listen */
  835. .listen = inet_dccp_listen,
  836. .shutdown = inet_shutdown,
  837. .setsockopt = sock_common_setsockopt,
  838. .getsockopt = sock_common_getsockopt,
  839. .sendmsg = inet_sendmsg,
  840. .recvmsg = sock_common_recvmsg,
  841. .mmap = sock_no_mmap,
  842. .sendpage = sock_no_sendpage,
  843. #ifdef CONFIG_COMPAT
  844. .compat_setsockopt = compat_sock_common_setsockopt,
  845. .compat_getsockopt = compat_sock_common_getsockopt,
  846. #endif
  847. };
  848. static struct inet_protosw dccp_v4_protosw = {
  849. .type = SOCK_DCCP,
  850. .protocol = IPPROTO_DCCP,
  851. .prot = &dccp_v4_prot,
  852. .ops = &inet_dccp_ops,
  853. .capability = -1,
  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");