ipv4.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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/config.h>
  13. #include <linux/dccp.h>
  14. #include <linux/icmp.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_hashtables.h>
  20. #include <net/inet_sock.h>
  21. #include <net/sock.h>
  22. #include <net/timewait_sock.h>
  23. #include <net/tcp_states.h>
  24. #include <net/xfrm.h>
  25. #include "ackvec.h"
  26. #include "ccid.h"
  27. #include "dccp.h"
  28. #include "feat.h"
  29. struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
  30. .lhash_lock = RW_LOCK_UNLOCKED,
  31. .lhash_users = ATOMIC_INIT(0),
  32. .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
  33. };
  34. EXPORT_SYMBOL_GPL(dccp_hashinfo);
  35. static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
  36. {
  37. return inet_csk_get_port(&dccp_hashinfo, sk, snum,
  38. inet_csk_bind_conflict);
  39. }
  40. int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  41. {
  42. struct inet_sock *inet = inet_sk(sk);
  43. struct dccp_sock *dp = dccp_sk(sk);
  44. const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  45. struct rtable *rt;
  46. u32 daddr, nexthop;
  47. int tmp;
  48. int err;
  49. dp->dccps_role = DCCP_ROLE_CLIENT;
  50. if (dccp_service_not_initialized(sk))
  51. return -EPROTO;
  52. if (addr_len < sizeof(struct sockaddr_in))
  53. return -EINVAL;
  54. if (usin->sin_family != AF_INET)
  55. return -EAFNOSUPPORT;
  56. nexthop = daddr = usin->sin_addr.s_addr;
  57. if (inet->opt != NULL && inet->opt->srr) {
  58. if (daddr == 0)
  59. return -EINVAL;
  60. nexthop = inet->opt->faddr;
  61. }
  62. tmp = ip_route_connect(&rt, nexthop, inet->saddr,
  63. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  64. IPPROTO_DCCP,
  65. inet->sport, usin->sin_port, sk);
  66. if (tmp < 0)
  67. return tmp;
  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->srr)
  73. daddr = rt->rt_dst;
  74. if (inet->saddr == 0)
  75. inet->saddr = rt->rt_src;
  76. inet->rcv_saddr = inet->saddr;
  77. inet->dport = usin->sin_port;
  78. inet->daddr = daddr;
  79. inet_csk(sk)->icsk_ext_hdr_len = 0;
  80. if (inet->opt != NULL)
  81. inet_csk(sk)->icsk_ext_hdr_len = inet->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. err = ip_route_newports(&rt, IPPROTO_DCCP, inet->sport, inet->dport,
  93. sk);
  94. if (err != 0)
  95. goto failure;
  96. /* OK, now commit destination to socket. */
  97. sk_setup_caps(sk, &rt->u.dst);
  98. dp->dccps_gar =
  99. dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
  100. inet->daddr,
  101. inet->sport,
  102. usin->sin_port);
  103. dccp_update_gss(sk, dp->dccps_iss);
  104. 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->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: draft-ietf-dccp-spec-11.txt
  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. static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
  167. {
  168. int err;
  169. struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
  170. const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
  171. sizeof(struct dccp_hdr_ext) +
  172. sizeof(struct dccp_hdr_ack_bits);
  173. struct sk_buff *skb;
  174. if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
  175. return;
  176. skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
  177. if (skb == NULL)
  178. return;
  179. /* Reserve space for headers. */
  180. skb_reserve(skb, MAX_DCCP_HEADER);
  181. skb->dst = dst_clone(rxskb->dst);
  182. skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
  183. dh = dccp_hdr(skb);
  184. memset(dh, 0, dccp_hdr_ack_len);
  185. /* Build DCCP header and checksum it. */
  186. dh->dccph_type = DCCP_PKT_ACK;
  187. dh->dccph_sport = rxdh->dccph_dport;
  188. dh->dccph_dport = rxdh->dccph_sport;
  189. dh->dccph_doff = dccp_hdr_ack_len / 4;
  190. dh->dccph_x = 1;
  191. dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
  192. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
  193. DCCP_SKB_CB(rxskb)->dccpd_seq);
  194. bh_lock_sock(dccp_ctl_socket->sk);
  195. err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
  196. rxskb->nh.iph->daddr,
  197. rxskb->nh.iph->saddr, NULL);
  198. bh_unlock_sock(dccp_ctl_socket->sk);
  199. if (err == NET_XMIT_CN || err == 0) {
  200. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  201. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  202. }
  203. }
  204. static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
  205. struct request_sock *req)
  206. {
  207. dccp_v4_ctl_send_ack(skb);
  208. }
  209. static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
  210. struct dst_entry *dst)
  211. {
  212. int err = -1;
  213. struct sk_buff *skb;
  214. /* First, grab a route. */
  215. if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
  216. goto out;
  217. skb = dccp_make_response(sk, dst, req);
  218. if (skb != NULL) {
  219. const struct inet_request_sock *ireq = inet_rsk(req);
  220. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  221. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  222. ireq->rmt_addr,
  223. ireq->opt);
  224. if (err == NET_XMIT_CN)
  225. err = 0;
  226. }
  227. out:
  228. dst_release(dst);
  229. return err;
  230. }
  231. /*
  232. * This routine is called by the ICMP module when it gets some sort of error
  233. * condition. If err < 0 then the socket should be closed and the error
  234. * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
  235. * After adjustment header points to the first 8 bytes of the tcp header. We
  236. * need to find the appropriate port.
  237. *
  238. * The locking strategy used here is very "optimistic". When someone else
  239. * accesses the socket the ICMP is just dropped and for some paths there is no
  240. * check at all. A more general error queue to queue errors for later handling
  241. * is probably better.
  242. */
  243. void dccp_v4_err(struct sk_buff *skb, u32 info)
  244. {
  245. const struct iphdr *iph = (struct iphdr *)skb->data;
  246. const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
  247. (iph->ihl << 2));
  248. struct dccp_sock *dp;
  249. struct inet_sock *inet;
  250. const int type = skb->h.icmph->type;
  251. const int code = skb->h.icmph->code;
  252. struct sock *sk;
  253. __u64 seq;
  254. int err;
  255. if (skb->len < (iph->ihl << 2) + 8) {
  256. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  257. return;
  258. }
  259. sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
  260. iph->saddr, dh->dccph_sport, inet_iif(skb));
  261. if (sk == NULL) {
  262. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  263. return;
  264. }
  265. if (sk->sk_state == DCCP_TIME_WAIT) {
  266. inet_twsk_put((struct inet_timewait_sock *)sk);
  267. return;
  268. }
  269. bh_lock_sock(sk);
  270. /* If too many ICMPs get dropped on busy
  271. * servers this needs to be solved differently.
  272. */
  273. if (sock_owned_by_user(sk))
  274. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  275. if (sk->sk_state == DCCP_CLOSED)
  276. goto out;
  277. dp = dccp_sk(sk);
  278. seq = dccp_hdr_seq(skb);
  279. if (sk->sk_state != DCCP_LISTEN &&
  280. !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
  281. NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
  282. goto out;
  283. }
  284. switch (type) {
  285. case ICMP_SOURCE_QUENCH:
  286. /* Just silently ignore these. */
  287. goto out;
  288. case ICMP_PARAMETERPROB:
  289. err = EPROTO;
  290. break;
  291. case ICMP_DEST_UNREACH:
  292. if (code > NR_ICMP_UNREACH)
  293. goto out;
  294. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  295. if (!sock_owned_by_user(sk))
  296. dccp_do_pmtu_discovery(sk, iph, info);
  297. goto out;
  298. }
  299. err = icmp_err_convert[code].errno;
  300. break;
  301. case ICMP_TIME_EXCEEDED:
  302. err = EHOSTUNREACH;
  303. break;
  304. default:
  305. goto out;
  306. }
  307. switch (sk->sk_state) {
  308. struct request_sock *req , **prev;
  309. case DCCP_LISTEN:
  310. if (sock_owned_by_user(sk))
  311. goto out;
  312. req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
  313. iph->daddr, iph->saddr);
  314. if (!req)
  315. goto out;
  316. /*
  317. * ICMPs are not backlogged, hence we cannot get an established
  318. * socket here.
  319. */
  320. BUG_TRAP(!req->sk);
  321. if (seq != dccp_rsk(req)->dreq_iss) {
  322. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  323. goto out;
  324. }
  325. /*
  326. * Still in RESPOND, just remove it silently.
  327. * There is no good way to pass the error to the newly
  328. * created socket, and POSIX does not want network
  329. * errors returned from accept().
  330. */
  331. inet_csk_reqsk_queue_drop(sk, req, prev);
  332. goto out;
  333. case DCCP_REQUESTING:
  334. case DCCP_RESPOND:
  335. if (!sock_owned_by_user(sk)) {
  336. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  337. sk->sk_err = err;
  338. sk->sk_error_report(sk);
  339. dccp_done(sk);
  340. } else
  341. sk->sk_err_soft = err;
  342. goto out;
  343. }
  344. /* If we've already connected we will keep trying
  345. * until we time out, or the user gives up.
  346. *
  347. * rfc1122 4.2.3.9 allows to consider as hard errors
  348. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  349. * but it is obsoleted by pmtu discovery).
  350. *
  351. * Note, that in modern internet, where routing is unreliable
  352. * and in each dark corner broken firewalls sit, sending random
  353. * errors ordered by their masters even this two messages finally lose
  354. * their original sense (even Linux sends invalid PORT_UNREACHs)
  355. *
  356. * Now we are in compliance with RFCs.
  357. * --ANK (980905)
  358. */
  359. inet = inet_sk(sk);
  360. if (!sock_owned_by_user(sk) && inet->recverr) {
  361. sk->sk_err = err;
  362. sk->sk_error_report(sk);
  363. } else /* Only an error on timeout */
  364. sk->sk_err_soft = err;
  365. out:
  366. bh_unlock_sock(sk);
  367. sock_put(sk);
  368. }
  369. /* This routine computes an IPv4 DCCP checksum. */
  370. void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
  371. {
  372. const struct inet_sock *inet = inet_sk(sk);
  373. struct dccp_hdr *dh = dccp_hdr(skb);
  374. dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
  375. }
  376. EXPORT_SYMBOL_GPL(dccp_v4_send_check);
  377. static inline u64 dccp_v4_init_sequence(const struct sock *sk,
  378. const struct sk_buff *skb)
  379. {
  380. return secure_dccp_sequence_number(skb->nh.iph->daddr,
  381. skb->nh.iph->saddr,
  382. dccp_hdr(skb)->dccph_dport,
  383. dccp_hdr(skb)->dccph_sport);
  384. }
  385. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  386. {
  387. struct inet_request_sock *ireq;
  388. struct dccp_sock dp;
  389. struct request_sock *req;
  390. struct dccp_request_sock *dreq;
  391. const __be32 saddr = skb->nh.iph->saddr;
  392. const __be32 daddr = skb->nh.iph->daddr;
  393. const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
  394. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  395. __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
  396. /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
  397. if (((struct rtable *)skb->dst)->rt_flags &
  398. (RTCF_BROADCAST | RTCF_MULTICAST)) {
  399. reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  400. goto drop;
  401. }
  402. if (dccp_bad_service_code(sk, service)) {
  403. reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
  404. goto drop;
  405. }
  406. /*
  407. * TW buckets are converted to open requests without
  408. * limitations, they conserve resources and peer is
  409. * evidently real one.
  410. */
  411. if (inet_csk_reqsk_queue_is_full(sk))
  412. goto drop;
  413. /*
  414. * Accept backlog is full. If we have already queued enough
  415. * of warm entries in syn queue, drop request. It is better than
  416. * clogging syn queue with openreqs with exponentially increasing
  417. * timeout.
  418. */
  419. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  420. goto drop;
  421. req = reqsk_alloc(sk->sk_prot->rsk_prot);
  422. if (req == NULL)
  423. goto drop;
  424. if (dccp_parse_options(sk, skb))
  425. goto drop;
  426. dccp_openreq_init(req, &dp, skb);
  427. ireq = inet_rsk(req);
  428. ireq->loc_addr = daddr;
  429. ireq->rmt_addr = saddr;
  430. req->rcv_wnd = 100; /* Fake, option parsing will get the
  431. right value */
  432. ireq->opt = NULL;
  433. /*
  434. * Step 3: Process LISTEN state
  435. *
  436. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  437. *
  438. * In fact we defer setting S.GSR, S.SWL, S.SWH to
  439. * dccp_create_openreq_child.
  440. */
  441. dreq = dccp_rsk(req);
  442. dreq->dreq_isr = dcb->dccpd_seq;
  443. dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
  444. dreq->dreq_service = service;
  445. if (dccp_v4_send_response(sk, req, NULL))
  446. goto drop_and_free;
  447. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  448. return 0;
  449. drop_and_free:
  450. reqsk_free(req);
  451. drop:
  452. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  453. dcb->dccpd_reset_code = reset_code;
  454. return -1;
  455. }
  456. EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
  457. /*
  458. * The three way handshake has completed - we got a valid ACK or DATAACK -
  459. * now create the new socket.
  460. *
  461. * This is the equivalent of TCP's tcp_v4_syn_recv_sock
  462. */
  463. struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
  464. struct request_sock *req,
  465. struct dst_entry *dst)
  466. {
  467. struct inet_request_sock *ireq;
  468. struct inet_sock *newinet;
  469. struct dccp_sock *newdp;
  470. struct sock *newsk;
  471. if (sk_acceptq_is_full(sk))
  472. goto exit_overflow;
  473. if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
  474. goto exit;
  475. newsk = dccp_create_openreq_child(sk, req, skb);
  476. if (newsk == NULL)
  477. goto exit;
  478. sk_setup_caps(newsk, dst);
  479. newdp = dccp_sk(newsk);
  480. newinet = inet_sk(newsk);
  481. ireq = inet_rsk(req);
  482. newinet->daddr = ireq->rmt_addr;
  483. newinet->rcv_saddr = ireq->loc_addr;
  484. newinet->saddr = ireq->loc_addr;
  485. newinet->opt = ireq->opt;
  486. ireq->opt = NULL;
  487. newinet->mc_index = inet_iif(skb);
  488. newinet->mc_ttl = skb->nh.iph->ttl;
  489. newinet->id = jiffies;
  490. dccp_sync_mss(newsk, dst_mtu(dst));
  491. __inet_hash(&dccp_hashinfo, newsk, 0);
  492. __inet_inherit_port(&dccp_hashinfo, sk, newsk);
  493. return newsk;
  494. exit_overflow:
  495. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  496. exit:
  497. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  498. dst_release(dst);
  499. return NULL;
  500. }
  501. EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
  502. static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  503. {
  504. const struct dccp_hdr *dh = dccp_hdr(skb);
  505. const struct iphdr *iph = skb->nh.iph;
  506. struct sock *nsk;
  507. struct request_sock **prev;
  508. /* Find possible connection requests. */
  509. struct request_sock *req = inet_csk_search_req(sk, &prev,
  510. dh->dccph_sport,
  511. iph->saddr, iph->daddr);
  512. if (req != NULL)
  513. return dccp_check_req(sk, skb, req, prev);
  514. nsk = __inet_lookup_established(&dccp_hashinfo,
  515. iph->saddr, dh->dccph_sport,
  516. iph->daddr, ntohs(dh->dccph_dport),
  517. inet_iif(skb));
  518. if (nsk != NULL) {
  519. if (nsk->sk_state != DCCP_TIME_WAIT) {
  520. bh_lock_sock(nsk);
  521. return nsk;
  522. }
  523. inet_twsk_put((struct inet_timewait_sock *)nsk);
  524. return NULL;
  525. }
  526. return sk;
  527. }
  528. int dccp_v4_checksum(const struct sk_buff *skb, const __be32 saddr,
  529. const __be32 daddr)
  530. {
  531. const struct dccp_hdr* dh = dccp_hdr(skb);
  532. int checksum_len;
  533. u32 tmp;
  534. if (dh->dccph_cscov == 0)
  535. checksum_len = skb->len;
  536. else {
  537. checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
  538. checksum_len = checksum_len < skb->len ? checksum_len :
  539. skb->len;
  540. }
  541. tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
  542. return csum_tcpudp_magic(saddr, daddr, checksum_len,
  543. IPPROTO_DCCP, tmp);
  544. }
  545. static int dccp_v4_verify_checksum(struct sk_buff *skb,
  546. const __be32 saddr, const __be32 daddr)
  547. {
  548. struct dccp_hdr *dh = dccp_hdr(skb);
  549. int checksum_len;
  550. u32 tmp;
  551. if (dh->dccph_cscov == 0)
  552. checksum_len = skb->len;
  553. else {
  554. checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
  555. checksum_len = checksum_len < skb->len ? checksum_len :
  556. skb->len;
  557. }
  558. tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
  559. return csum_tcpudp_magic(saddr, daddr, checksum_len,
  560. IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
  561. }
  562. static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
  563. struct sk_buff *skb)
  564. {
  565. struct rtable *rt;
  566. struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
  567. .nl_u = { .ip4_u =
  568. { .daddr = skb->nh.iph->saddr,
  569. .saddr = skb->nh.iph->daddr,
  570. .tos = RT_CONN_FLAGS(sk) } },
  571. .proto = sk->sk_protocol,
  572. .uli_u = { .ports =
  573. { .sport = dccp_hdr(skb)->dccph_dport,
  574. .dport = dccp_hdr(skb)->dccph_sport }
  575. }
  576. };
  577. if (ip_route_output_flow(&rt, &fl, sk, 0)) {
  578. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  579. return NULL;
  580. }
  581. return &rt->u.dst;
  582. }
  583. static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
  584. {
  585. int err;
  586. struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
  587. const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
  588. sizeof(struct dccp_hdr_ext) +
  589. sizeof(struct dccp_hdr_reset);
  590. struct sk_buff *skb;
  591. struct dst_entry *dst;
  592. u64 seqno;
  593. /* Never send a reset in response to a reset. */
  594. if (rxdh->dccph_type == DCCP_PKT_RESET)
  595. return;
  596. if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
  597. return;
  598. dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
  599. if (dst == NULL)
  600. return;
  601. skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
  602. if (skb == NULL)
  603. goto out;
  604. /* Reserve space for headers. */
  605. skb_reserve(skb, MAX_DCCP_HEADER);
  606. skb->dst = dst_clone(dst);
  607. skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
  608. dh = dccp_hdr(skb);
  609. memset(dh, 0, dccp_hdr_reset_len);
  610. /* Build DCCP header and checksum it. */
  611. dh->dccph_type = DCCP_PKT_RESET;
  612. dh->dccph_sport = rxdh->dccph_dport;
  613. dh->dccph_dport = rxdh->dccph_sport;
  614. dh->dccph_doff = dccp_hdr_reset_len / 4;
  615. dh->dccph_x = 1;
  616. dccp_hdr_reset(skb)->dccph_reset_code =
  617. DCCP_SKB_CB(rxskb)->dccpd_reset_code;
  618. /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
  619. seqno = 0;
  620. if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  621. dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
  622. dccp_hdr_set_seq(dh, seqno);
  623. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
  624. DCCP_SKB_CB(rxskb)->dccpd_seq);
  625. dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
  626. rxskb->nh.iph->daddr);
  627. bh_lock_sock(dccp_ctl_socket->sk);
  628. err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
  629. rxskb->nh.iph->daddr,
  630. rxskb->nh.iph->saddr, NULL);
  631. bh_unlock_sock(dccp_ctl_socket->sk);
  632. if (err == NET_XMIT_CN || err == 0) {
  633. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  634. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  635. }
  636. out:
  637. dst_release(dst);
  638. }
  639. int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  640. {
  641. struct dccp_hdr *dh = dccp_hdr(skb);
  642. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  643. if (dccp_rcv_established(sk, skb, dh, skb->len))
  644. goto reset;
  645. return 0;
  646. }
  647. /*
  648. * Step 3: Process LISTEN state
  649. * If S.state == LISTEN,
  650. * If P.type == Request or P contains a valid Init Cookie
  651. * option,
  652. * * Must scan the packet's options to check for an Init
  653. * Cookie. Only the Init Cookie is processed here,
  654. * however; other options are processed in Step 8. This
  655. * scan need only be performed if the endpoint uses Init
  656. * Cookies *
  657. * * Generate a new socket and switch to that socket *
  658. * Set S := new socket for this port pair
  659. * S.state = RESPOND
  660. * Choose S.ISS (initial seqno) or set from Init Cookie
  661. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  662. * Continue with S.state == RESPOND
  663. * * A Response packet will be generated in Step 11 *
  664. * Otherwise,
  665. * Generate Reset(No Connection) unless P.type == Reset
  666. * Drop packet and return
  667. *
  668. * NOTE: the check for the packet types is done in
  669. * dccp_rcv_state_process
  670. */
  671. if (sk->sk_state == DCCP_LISTEN) {
  672. struct sock *nsk = dccp_v4_hnd_req(sk, skb);
  673. if (nsk == NULL)
  674. goto discard;
  675. if (nsk != sk) {
  676. if (dccp_child_process(sk, nsk, skb))
  677. goto reset;
  678. return 0;
  679. }
  680. }
  681. if (dccp_rcv_state_process(sk, skb, dh, skb->len))
  682. goto reset;
  683. return 0;
  684. reset:
  685. dccp_v4_ctl_send_reset(skb);
  686. discard:
  687. kfree_skb(skb);
  688. return 0;
  689. }
  690. EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
  691. int dccp_invalid_packet(struct sk_buff *skb)
  692. {
  693. const struct dccp_hdr *dh;
  694. if (skb->pkt_type != PACKET_HOST)
  695. return 1;
  696. if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
  697. LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
  698. return 1;
  699. }
  700. dh = dccp_hdr(skb);
  701. /* If the packet type is not understood, drop packet and return */
  702. if (dh->dccph_type >= DCCP_PKT_INVALID) {
  703. LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
  704. return 1;
  705. }
  706. /*
  707. * If P.Data Offset is too small for packet type, or too large for
  708. * packet, drop packet and return
  709. */
  710. if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
  711. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
  712. "too small 1\n",
  713. dh->dccph_doff);
  714. return 1;
  715. }
  716. if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
  717. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
  718. "too small 2\n",
  719. dh->dccph_doff);
  720. return 1;
  721. }
  722. dh = dccp_hdr(skb);
  723. /*
  724. * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
  725. * has short sequence numbers), drop packet and return
  726. */
  727. if (dh->dccph_x == 0 &&
  728. dh->dccph_type != DCCP_PKT_DATA &&
  729. dh->dccph_type != DCCP_PKT_ACK &&
  730. dh->dccph_type != DCCP_PKT_DATAACK) {
  731. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
  732. "nor DataAck and P.X == 0\n",
  733. dccp_packet_name(dh->dccph_type));
  734. return 1;
  735. }
  736. return 0;
  737. }
  738. EXPORT_SYMBOL_GPL(dccp_invalid_packet);
  739. /* this is called when real data arrives */
  740. int dccp_v4_rcv(struct sk_buff *skb)
  741. {
  742. const struct dccp_hdr *dh;
  743. struct sock *sk;
  744. /* Step 1: Check header basics: */
  745. if (dccp_invalid_packet(skb))
  746. goto discard_it;
  747. /* If the header checksum is incorrect, drop packet and return */
  748. if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
  749. skb->nh.iph->daddr) < 0) {
  750. LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
  751. __FUNCTION__);
  752. goto discard_it;
  753. }
  754. dh = dccp_hdr(skb);
  755. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
  756. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  757. dccp_pr_debug("%8.8s "
  758. "src=%u.%u.%u.%u@%-5d "
  759. "dst=%u.%u.%u.%u@%-5d seq=%llu",
  760. dccp_packet_name(dh->dccph_type),
  761. NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
  762. NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
  763. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
  764. if (dccp_packet_without_ack(skb)) {
  765. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  766. dccp_pr_debug_cat("\n");
  767. } else {
  768. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  769. dccp_pr_debug_cat(", ack=%llu\n",
  770. (unsigned long long)
  771. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  772. }
  773. /* Step 2:
  774. * Look up flow ID in table and get corresponding socket */
  775. sk = __inet_lookup(&dccp_hashinfo,
  776. skb->nh.iph->saddr, dh->dccph_sport,
  777. skb->nh.iph->daddr, ntohs(dh->dccph_dport),
  778. inet_iif(skb));
  779. /*
  780. * Step 2:
  781. * If no socket ...
  782. * Generate Reset(No Connection) unless P.type == Reset
  783. * Drop packet and return
  784. */
  785. if (sk == NULL) {
  786. dccp_pr_debug("failed to look up flow ID in table and "
  787. "get corresponding socket\n");
  788. goto no_dccp_socket;
  789. }
  790. /*
  791. * Step 2:
  792. * ... or S.state == TIMEWAIT,
  793. * Generate Reset(No Connection) unless P.type == Reset
  794. * Drop packet and return
  795. */
  796. if (sk->sk_state == DCCP_TIME_WAIT) {
  797. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
  798. "do_time_wait\n");
  799. goto do_time_wait;
  800. }
  801. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  802. goto discard_and_relse;
  803. nf_reset(skb);
  804. return sk_receive_skb(sk, skb);
  805. no_dccp_socket:
  806. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  807. goto discard_it;
  808. /*
  809. * Step 2:
  810. * Generate Reset(No Connection) unless P.type == Reset
  811. * Drop packet and return
  812. */
  813. if (dh->dccph_type != DCCP_PKT_RESET) {
  814. DCCP_SKB_CB(skb)->dccpd_reset_code =
  815. DCCP_RESET_CODE_NO_CONNECTION;
  816. dccp_v4_ctl_send_reset(skb);
  817. }
  818. discard_it:
  819. /* Discard frame. */
  820. kfree_skb(skb);
  821. return 0;
  822. discard_and_relse:
  823. sock_put(sk);
  824. goto discard_it;
  825. do_time_wait:
  826. inet_twsk_put((struct inet_timewait_sock *)sk);
  827. goto no_dccp_socket;
  828. }
  829. struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
  830. .queue_xmit = ip_queue_xmit,
  831. .send_check = dccp_v4_send_check,
  832. .rebuild_header = inet_sk_rebuild_header,
  833. .conn_request = dccp_v4_conn_request,
  834. .syn_recv_sock = dccp_v4_request_recv_sock,
  835. .net_header_len = sizeof(struct iphdr),
  836. .setsockopt = ip_setsockopt,
  837. .getsockopt = ip_getsockopt,
  838. .addr2sockaddr = inet_csk_addr2sockaddr,
  839. .sockaddr_len = sizeof(struct sockaddr_in),
  840. };
  841. static int dccp_v4_init_sock(struct sock *sk)
  842. {
  843. const int err = dccp_init_sock(sk);
  844. if (err == 0)
  845. inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
  846. return err;
  847. }
  848. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  849. {
  850. kfree(inet_rsk(req)->opt);
  851. }
  852. static struct request_sock_ops dccp_request_sock_ops = {
  853. .family = PF_INET,
  854. .obj_size = sizeof(struct dccp_request_sock),
  855. .rtx_syn_ack = dccp_v4_send_response,
  856. .send_ack = dccp_v4_reqsk_send_ack,
  857. .destructor = dccp_v4_reqsk_destructor,
  858. .send_reset = dccp_v4_ctl_send_reset,
  859. };
  860. static struct timewait_sock_ops dccp_timewait_sock_ops = {
  861. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  862. };
  863. struct proto dccp_prot = {
  864. .name = "DCCP",
  865. .owner = THIS_MODULE,
  866. .close = dccp_close,
  867. .connect = dccp_v4_connect,
  868. .disconnect = dccp_disconnect,
  869. .ioctl = dccp_ioctl,
  870. .init = dccp_v4_init_sock,
  871. .setsockopt = dccp_setsockopt,
  872. .getsockopt = dccp_getsockopt,
  873. .sendmsg = dccp_sendmsg,
  874. .recvmsg = dccp_recvmsg,
  875. .backlog_rcv = dccp_v4_do_rcv,
  876. .hash = dccp_hash,
  877. .unhash = dccp_unhash,
  878. .accept = inet_csk_accept,
  879. .get_port = dccp_v4_get_port,
  880. .shutdown = dccp_shutdown,
  881. .destroy = dccp_destroy_sock,
  882. .orphan_count = &dccp_orphan_count,
  883. .max_header = MAX_DCCP_HEADER,
  884. .obj_size = sizeof(struct dccp_sock),
  885. .rsk_prot = &dccp_request_sock_ops,
  886. .twsk_prot = &dccp_timewait_sock_ops,
  887. };
  888. EXPORT_SYMBOL_GPL(dccp_prot);