ipv4.c 29 KB

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