datagram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  22. {
  23. struct inet_sock *inet = inet_sk(sk);
  24. struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
  25. struct flowi4 *fl4;
  26. struct rtable *rt;
  27. __be32 saddr;
  28. int oif;
  29. int err;
  30. if (addr_len < sizeof(*usin))
  31. return -EINVAL;
  32. if (usin->sin_family != AF_INET)
  33. return -EAFNOSUPPORT;
  34. sk_dst_reset(sk);
  35. oif = sk->sk_bound_dev_if;
  36. saddr = inet->inet_saddr;
  37. if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
  38. if (!oif)
  39. oif = inet->mc_index;
  40. if (!saddr)
  41. saddr = inet->mc_addr;
  42. }
  43. fl4 = &inet->cork.fl.u.ip4;
  44. rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
  45. RT_CONN_FLAGS(sk), oif,
  46. sk->sk_protocol,
  47. inet->inet_sport, usin->sin_port, sk);
  48. if (IS_ERR(rt)) {
  49. err = PTR_ERR(rt);
  50. if (err == -ENETUNREACH)
  51. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  52. goto out;
  53. }
  54. if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
  55. ip_rt_put(rt);
  56. err = -EACCES;
  57. goto out;
  58. }
  59. if (!inet->inet_saddr)
  60. inet->inet_saddr = fl4->saddr; /* Update source address */
  61. if (!inet->inet_rcv_saddr) {
  62. inet->inet_rcv_saddr = fl4->saddr;
  63. if (sk->sk_prot->rehash)
  64. sk->sk_prot->rehash(sk);
  65. }
  66. inet->inet_daddr = fl4->daddr;
  67. inet->inet_dport = usin->sin_port;
  68. sk->sk_state = TCP_ESTABLISHED;
  69. sk_set_txhash(sk);
  70. inet->inet_id = jiffies;
  71. sk_dst_set(sk, &rt->dst);
  72. err = 0;
  73. out:
  74. return err;
  75. }
  76. EXPORT_SYMBOL(__ip4_datagram_connect);
  77. int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  78. {
  79. int res;
  80. lock_sock(sk);
  81. res = __ip4_datagram_connect(sk, uaddr, addr_len);
  82. release_sock(sk);
  83. return res;
  84. }
  85. EXPORT_SYMBOL(ip4_datagram_connect);
  86. /* Because UDP xmit path can manipulate sk_dst_cache without holding
  87. * socket lock, we need to use sk_dst_set() here,
  88. * even if we own the socket lock.
  89. */
  90. void ip4_datagram_release_cb(struct sock *sk)
  91. {
  92. const struct inet_sock *inet = inet_sk(sk);
  93. const struct ip_options_rcu *inet_opt;
  94. __be32 daddr = inet->inet_daddr;
  95. struct dst_entry *dst;
  96. struct flowi4 fl4;
  97. struct rtable *rt;
  98. rcu_read_lock();
  99. dst = __sk_dst_get(sk);
  100. if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
  101. rcu_read_unlock();
  102. return;
  103. }
  104. inet_opt = rcu_dereference(inet->inet_opt);
  105. if (inet_opt && inet_opt->opt.srr)
  106. daddr = inet_opt->opt.faddr;
  107. rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
  108. inet->inet_saddr, inet->inet_dport,
  109. inet->inet_sport, sk->sk_protocol,
  110. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
  111. dst = !IS_ERR(rt) ? &rt->dst : NULL;
  112. sk_dst_set(sk, dst);
  113. rcu_read_unlock();
  114. }
  115. EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);