ipv4.c 30 KB

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