ipv4.c 28 KB

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