ipv4.c 30 KB

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