ipv4.c 30 KB

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