datagram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * common UDP/RAW code
  3. * Linux INET implementation
  4. *
  5. * Authors:
  6. * Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/module.h>
  15. #include <linux/ip.h>
  16. #include <linux/in.h>
  17. #include <net/ip.h>
  18. #include <net/sock.h>
  19. #include <net/route.h>
  20. #include <net/tcp_states.h>
  21. #include <net/sock_reuseport.h>
  22. int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  23. {
  24. struct inet_sock *inet = inet_sk(sk);
  25. struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
  26. struct flowi4 *fl4;
  27. struct rtable *rt;
  28. __be32 saddr;
  29. int oif;
  30. int err;
  31. if (addr_len < sizeof(*usin))
  32. return -EINVAL;
  33. if (usin->sin_family != AF_INET)
  34. return -EAFNOSUPPORT;
  35. sk_dst_reset(sk);
  36. oif = sk->sk_bound_dev_if;
  37. saddr = inet->inet_saddr;
  38. if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
  39. if (!oif)
  40. oif = inet->mc_index;
  41. if (!saddr)
  42. saddr = inet->mc_addr;
  43. }
  44. fl4 = &inet->cork.fl.u.ip4;
  45. rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
  46. RT_CONN_FLAGS(sk), oif,
  47. sk->sk_protocol,
  48. inet->inet_sport, usin->sin_port, sk);
  49. if (IS_ERR(rt)) {
  50. err = PTR_ERR(rt);
  51. if (err == -ENETUNREACH)
  52. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  53. goto out;
  54. }
  55. if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
  56. ip_rt_put(rt);
  57. err = -EACCES;
  58. goto out;
  59. }
  60. if (!inet->inet_saddr)
  61. inet->inet_saddr = fl4->saddr; /* Update source address */
  62. if (!inet->inet_rcv_saddr) {
  63. inet->inet_rcv_saddr = fl4->saddr;
  64. if (sk->sk_prot->rehash)
  65. sk->sk_prot->rehash(sk);
  66. }
  67. inet->inet_daddr = fl4->daddr;
  68. inet->inet_dport = usin->sin_port;
  69. reuseport_has_conns(sk, true);
  70. sk->sk_state = TCP_ESTABLISHED;
  71. sk_set_txhash(sk);
  72. inet->inet_id = prandom_u32();
  73. sk_dst_set(sk, &rt->dst);
  74. err = 0;
  75. out:
  76. return err;
  77. }
  78. EXPORT_SYMBOL(__ip4_datagram_connect);
  79. int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  80. {
  81. int res;
  82. lock_sock(sk);
  83. res = __ip4_datagram_connect(sk, uaddr, addr_len);
  84. release_sock(sk);
  85. return res;
  86. }
  87. EXPORT_SYMBOL(ip4_datagram_connect);
  88. /* Because UDP xmit path can manipulate sk_dst_cache without holding
  89. * socket lock, we need to use sk_dst_set() here,
  90. * even if we own the socket lock.
  91. */
  92. void ip4_datagram_release_cb(struct sock *sk)
  93. {
  94. const struct inet_sock *inet = inet_sk(sk);
  95. const struct ip_options_rcu *inet_opt;
  96. __be32 daddr = inet->inet_daddr;
  97. struct dst_entry *dst;
  98. struct flowi4 fl4;
  99. struct rtable *rt;
  100. rcu_read_lock();
  101. dst = __sk_dst_get(sk);
  102. if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
  103. rcu_read_unlock();
  104. return;
  105. }
  106. inet_opt = rcu_dereference(inet->inet_opt);
  107. if (inet_opt && inet_opt->opt.srr)
  108. daddr = inet_opt->opt.faddr;
  109. rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
  110. inet->inet_saddr, inet->inet_dport,
  111. inet->inet_sport, sk->sk_protocol,
  112. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
  113. dst = !IS_ERR(rt) ? &rt->dst : NULL;
  114. sk_dst_set(sk, dst);
  115. rcu_read_unlock();
  116. }
  117. EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);