ipv4.c 28 KB

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