ipv4.c 30 KB

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