ipv4.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. nf_reset(skb);
  831. return sk_receive_skb(sk, skb);
  832. no_dccp_socket:
  833. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  834. goto discard_it;
  835. /*
  836. * Step 2:
  837. * Generate Reset(No Connection) unless P.type == Reset
  838. * Drop packet and return
  839. */
  840. if (dh->dccph_type != DCCP_PKT_RESET) {
  841. DCCP_SKB_CB(skb)->dccpd_reset_code =
  842. DCCP_RESET_CODE_NO_CONNECTION;
  843. dccp_v4_ctl_send_reset(skb);
  844. }
  845. discard_it:
  846. /* Discard frame. */
  847. kfree_skb(skb);
  848. return 0;
  849. discard_and_relse:
  850. sock_put(sk);
  851. goto discard_it;
  852. do_time_wait:
  853. inet_twsk_put((struct inet_timewait_sock *)sk);
  854. goto no_dccp_socket;
  855. }
  856. struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
  857. .queue_xmit = ip_queue_xmit,
  858. .send_check = dccp_v4_send_check,
  859. .rebuild_header = inet_sk_rebuild_header,
  860. .conn_request = dccp_v4_conn_request,
  861. .syn_recv_sock = dccp_v4_request_recv_sock,
  862. .net_header_len = sizeof(struct iphdr),
  863. .setsockopt = ip_setsockopt,
  864. .getsockopt = ip_getsockopt,
  865. .addr2sockaddr = inet_csk_addr2sockaddr,
  866. .sockaddr_len = sizeof(struct sockaddr_in),
  867. };
  868. int dccp_v4_init_sock(struct sock *sk)
  869. {
  870. struct dccp_sock *dp = dccp_sk(sk);
  871. struct inet_connection_sock *icsk = inet_csk(sk);
  872. static int dccp_ctl_socket_init = 1;
  873. dccp_options_init(&dp->dccps_options);
  874. do_gettimeofday(&dp->dccps_epoch);
  875. if (dp->dccps_options.dccpo_send_ack_vector) {
  876. dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
  877. GFP_KERNEL);
  878. if (dp->dccps_hc_rx_ackvec == NULL)
  879. return -ENOMEM;
  880. }
  881. /*
  882. * FIXME: We're hardcoding the CCID, and doing this at this point makes
  883. * the listening (master) sock get CCID control blocks, which is not
  884. * necessary, but for now, to not mess with the test userspace apps,
  885. * lets leave it here, later the real solution is to do this in a
  886. * setsockopt(CCIDs-I-want/accept). -acme
  887. */
  888. if (likely(!dccp_ctl_socket_init)) {
  889. dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
  890. sk);
  891. dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
  892. sk);
  893. if (dp->dccps_hc_rx_ccid == NULL ||
  894. dp->dccps_hc_tx_ccid == NULL) {
  895. ccid_exit(dp->dccps_hc_rx_ccid, sk);
  896. ccid_exit(dp->dccps_hc_tx_ccid, sk);
  897. if (dp->dccps_options.dccpo_send_ack_vector) {
  898. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  899. dp->dccps_hc_rx_ackvec = NULL;
  900. }
  901. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  902. return -ENOMEM;
  903. }
  904. } else
  905. dccp_ctl_socket_init = 0;
  906. dccp_init_xmit_timers(sk);
  907. icsk->icsk_rto = DCCP_TIMEOUT_INIT;
  908. sk->sk_state = DCCP_CLOSED;
  909. sk->sk_write_space = dccp_write_space;
  910. icsk->icsk_af_ops = &dccp_ipv4_af_ops;
  911. icsk->icsk_sync_mss = dccp_sync_mss;
  912. dp->dccps_mss_cache = 536;
  913. dp->dccps_role = DCCP_ROLE_UNDEFINED;
  914. dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
  915. return 0;
  916. }
  917. EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
  918. int dccp_v4_destroy_sock(struct sock *sk)
  919. {
  920. struct dccp_sock *dp = dccp_sk(sk);
  921. /*
  922. * DCCP doesn't use sk_write_queue, just sk_send_head
  923. * for retransmissions
  924. */
  925. if (sk->sk_send_head != NULL) {
  926. kfree_skb(sk->sk_send_head);
  927. sk->sk_send_head = NULL;
  928. }
  929. /* Clean up a referenced DCCP bind bucket. */
  930. if (inet_csk(sk)->icsk_bind_hash != NULL)
  931. inet_put_port(&dccp_hashinfo, sk);
  932. kfree(dp->dccps_service_list);
  933. dp->dccps_service_list = NULL;
  934. ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
  935. ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
  936. if (dp->dccps_options.dccpo_send_ack_vector) {
  937. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  938. dp->dccps_hc_rx_ackvec = NULL;
  939. }
  940. ccid_exit(dp->dccps_hc_rx_ccid, sk);
  941. ccid_exit(dp->dccps_hc_tx_ccid, sk);
  942. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  943. return 0;
  944. }
  945. EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
  946. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  947. {
  948. kfree(inet_rsk(req)->opt);
  949. }
  950. static struct request_sock_ops dccp_request_sock_ops = {
  951. .family = PF_INET,
  952. .obj_size = sizeof(struct dccp_request_sock),
  953. .rtx_syn_ack = dccp_v4_send_response,
  954. .send_ack = dccp_v4_reqsk_send_ack,
  955. .destructor = dccp_v4_reqsk_destructor,
  956. .send_reset = dccp_v4_ctl_send_reset,
  957. };
  958. static struct timewait_sock_ops dccp_timewait_sock_ops = {
  959. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  960. };
  961. struct proto dccp_prot = {
  962. .name = "DCCP",
  963. .owner = THIS_MODULE,
  964. .close = dccp_close,
  965. .connect = dccp_v4_connect,
  966. .disconnect = dccp_disconnect,
  967. .ioctl = dccp_ioctl,
  968. .init = dccp_v4_init_sock,
  969. .setsockopt = dccp_setsockopt,
  970. .getsockopt = dccp_getsockopt,
  971. .sendmsg = dccp_sendmsg,
  972. .recvmsg = dccp_recvmsg,
  973. .backlog_rcv = dccp_v4_do_rcv,
  974. .hash = dccp_v4_hash,
  975. .unhash = dccp_unhash,
  976. .accept = inet_csk_accept,
  977. .get_port = dccp_v4_get_port,
  978. .shutdown = dccp_shutdown,
  979. .destroy = dccp_v4_destroy_sock,
  980. .orphan_count = &dccp_orphan_count,
  981. .max_header = MAX_DCCP_HEADER,
  982. .obj_size = sizeof(struct dccp_sock),
  983. .rsk_prot = &dccp_request_sock_ops,
  984. .twsk_prot = &dccp_timewait_sock_ops,
  985. };
  986. EXPORT_SYMBOL_GPL(dccp_prot);