ipv4.c 28 KB

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