ipv4.c 30 KB

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