ipv4.c 28 KB

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