ipv4.c 30 KB

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