ipv4.c 28 KB

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