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