route.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for the IP router.
  7. *
  8. * Version: @(#)route.h 1.0.4 05/27/93
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Fixes:
  13. * Alan Cox : Reformatted. Added ip_rt_local()
  14. * Alan Cox : Support for TCP parameters.
  15. * Alexey Kuznetsov: Major changes for new routing code.
  16. * Mike McLagan : Routing by source
  17. * Robert Olsson : Added rt_cache statistics
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. */
  24. #ifndef _ROUTE_H
  25. #define _ROUTE_H
  26. #include <net/dst.h>
  27. #include <net/inetpeer.h>
  28. #include <net/flow.h>
  29. #include <net/inet_sock.h>
  30. #include <linux/in_route.h>
  31. #include <linux/rtnetlink.h>
  32. #include <linux/route.h>
  33. #include <linux/ip.h>
  34. #include <linux/cache.h>
  35. #include <linux/security.h>
  36. #define RTO_ONLINK 0x01
  37. #define RTO_CONN 0
  38. /* RTO_CONN is not used (being alias for 0), but preserved not to break
  39. * some modules referring to it. */
  40. #define RT_CONN_FLAGS(sk) (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE))
  41. struct fib_nh;
  42. struct inet_peer;
  43. struct fib_info;
  44. struct rtable {
  45. struct dst_entry dst;
  46. /* Lookup key. */
  47. __be32 rt_key_dst;
  48. __be32 rt_key_src;
  49. int rt_genid;
  50. unsigned rt_flags;
  51. __u16 rt_type;
  52. __u8 rt_tos;
  53. __be32 rt_dst; /* Path destination */
  54. __be32 rt_src; /* Path source */
  55. int rt_route_iif;
  56. int rt_iif;
  57. int rt_oif;
  58. __u32 rt_mark;
  59. /* Info on neighbour */
  60. __be32 rt_gateway;
  61. /* Miscellaneous cached information */
  62. __be32 rt_spec_dst; /* RFC1122 specific destination */
  63. u32 rt_peer_genid;
  64. struct inet_peer *peer; /* long-living peer info */
  65. struct fib_info *fi; /* for client ref to shared metrics */
  66. };
  67. static inline bool rt_is_input_route(struct rtable *rt)
  68. {
  69. return rt->rt_route_iif != 0;
  70. }
  71. static inline bool rt_is_output_route(struct rtable *rt)
  72. {
  73. return rt->rt_route_iif == 0;
  74. }
  75. struct ip_rt_acct {
  76. __u32 o_bytes;
  77. __u32 o_packets;
  78. __u32 i_bytes;
  79. __u32 i_packets;
  80. };
  81. struct rt_cache_stat {
  82. unsigned int in_hit;
  83. unsigned int in_slow_tot;
  84. unsigned int in_slow_mc;
  85. unsigned int in_no_route;
  86. unsigned int in_brd;
  87. unsigned int in_martian_dst;
  88. unsigned int in_martian_src;
  89. unsigned int out_hit;
  90. unsigned int out_slow_tot;
  91. unsigned int out_slow_mc;
  92. unsigned int gc_total;
  93. unsigned int gc_ignored;
  94. unsigned int gc_goal_miss;
  95. unsigned int gc_dst_overflow;
  96. unsigned int in_hlist_search;
  97. unsigned int out_hlist_search;
  98. };
  99. extern struct ip_rt_acct __percpu *ip_rt_acct;
  100. struct in_device;
  101. extern int ip_rt_init(void);
  102. extern void ip_rt_redirect(__be32 old_gw, __be32 dst, __be32 new_gw,
  103. __be32 src, struct net_device *dev);
  104. extern void rt_cache_flush(struct net *net, int how);
  105. extern void rt_cache_flush_batch(struct net *net);
  106. extern struct rtable *__ip_route_output_key(struct net *, const struct flowi4 *flp);
  107. extern struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp,
  108. struct sock *sk);
  109. extern struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig);
  110. static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp)
  111. {
  112. return ip_route_output_flow(net, flp, NULL);
  113. }
  114. static inline struct rtable *ip_route_output(struct net *net, __be32 daddr,
  115. __be32 saddr, u8 tos, int oif)
  116. {
  117. struct flowi4 fl4 = {
  118. .flowi4_oif = oif,
  119. .daddr = daddr,
  120. .saddr = saddr,
  121. .flowi4_tos = tos,
  122. };
  123. return ip_route_output_key(net, &fl4);
  124. }
  125. static inline struct rtable *ip_route_output_ports(struct net *net, struct sock *sk,
  126. __be32 daddr, __be32 saddr,
  127. __be16 dport, __be16 sport,
  128. __u8 proto, __u8 tos, int oif)
  129. {
  130. struct flowi4 fl4;
  131. flowi4_init_output(&fl4, oif, sk ? sk->sk_mark : 0, tos,
  132. RT_SCOPE_UNIVERSE, proto,
  133. sk ? inet_sk_flowi_flags(sk) : 0,
  134. daddr, saddr, dport, sport);
  135. if (sk)
  136. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  137. return ip_route_output_flow(net, &fl4, sk);
  138. }
  139. static inline struct rtable *ip_route_output_gre(struct net *net,
  140. __be32 daddr, __be32 saddr,
  141. __be32 gre_key, __u8 tos, int oif)
  142. {
  143. struct flowi4 fl4 = {
  144. .flowi4_oif = oif,
  145. .daddr = daddr,
  146. .saddr = saddr,
  147. .flowi4_tos = tos,
  148. .flowi4_proto = IPPROTO_GRE,
  149. .fl4_gre_key = gre_key,
  150. };
  151. return ip_route_output_key(net, &fl4);
  152. }
  153. extern int ip_route_input_common(struct sk_buff *skb, __be32 dst, __be32 src,
  154. u8 tos, struct net_device *devin, bool noref);
  155. static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src,
  156. u8 tos, struct net_device *devin)
  157. {
  158. return ip_route_input_common(skb, dst, src, tos, devin, false);
  159. }
  160. static inline int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src,
  161. u8 tos, struct net_device *devin)
  162. {
  163. return ip_route_input_common(skb, dst, src, tos, devin, true);
  164. }
  165. extern unsigned short ip_rt_frag_needed(struct net *net, const struct iphdr *iph,
  166. unsigned short new_mtu, struct net_device *dev);
  167. extern void ip_rt_send_redirect(struct sk_buff *skb);
  168. extern unsigned inet_addr_type(struct net *net, __be32 addr);
  169. extern unsigned inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr);
  170. extern void ip_rt_multicast_event(struct in_device *);
  171. extern int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg);
  172. extern void ip_rt_get_source(u8 *src, struct rtable *rt);
  173. extern int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb);
  174. struct in_ifaddr;
  175. extern void fib_add_ifaddr(struct in_ifaddr *);
  176. extern void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *);
  177. static inline void ip_rt_put(struct rtable * rt)
  178. {
  179. if (rt)
  180. dst_release(&rt->dst);
  181. }
  182. #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3)
  183. extern const __u8 ip_tos2prio[16];
  184. static inline char rt_tos2priority(u8 tos)
  185. {
  186. return ip_tos2prio[IPTOS_TOS(tos)>>1];
  187. }
  188. static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos,
  189. int oif, u8 protocol,
  190. __be16 sport, __be16 dport,
  191. struct sock *sk, bool can_sleep)
  192. {
  193. struct net *net = sock_net(sk);
  194. struct rtable *rt;
  195. struct flowi4 fl4;
  196. __u8 flow_flags;
  197. flow_flags = 0;
  198. if (inet_sk(sk)->transparent)
  199. flow_flags |= FLOWI_FLAG_ANYSRC;
  200. if (protocol == IPPROTO_TCP)
  201. flow_flags |= FLOWI_FLAG_PRECOW_METRICS;
  202. if (can_sleep)
  203. flow_flags |= FLOWI_FLAG_CAN_SLEEP;
  204. flowi4_init_output(&fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE,
  205. protocol, flow_flags, dst, src, dport, sport);
  206. if (!dst || !src) {
  207. rt = __ip_route_output_key(net, &fl4);
  208. if (IS_ERR(rt))
  209. return rt;
  210. fl4.daddr = rt->rt_dst;
  211. fl4.saddr = rt->rt_src;
  212. ip_rt_put(rt);
  213. }
  214. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  215. return ip_route_output_flow(net, &fl4, sk);
  216. }
  217. static inline struct rtable *ip_route_newports(struct rtable *rt,
  218. u8 protocol, __be16 orig_sport,
  219. __be16 orig_dport, __be16 sport,
  220. __be16 dport, struct sock *sk)
  221. {
  222. if (sport != orig_sport || dport != orig_dport) {
  223. struct flowi4 fl4;
  224. __u8 flow_flags;
  225. flow_flags = 0;
  226. if (inet_sk(sk)->transparent)
  227. flow_flags |= FLOWI_FLAG_ANYSRC;
  228. if (protocol == IPPROTO_TCP)
  229. flow_flags |= FLOWI_FLAG_PRECOW_METRICS;
  230. flowi4_init_output(&fl4, rt->rt_oif, rt->rt_mark, rt->rt_tos,
  231. RT_SCOPE_UNIVERSE, protocol, flow_flags,
  232. rt->rt_dst, rt->rt_src, dport, sport);
  233. ip_rt_put(rt);
  234. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  235. return ip_route_output_flow(sock_net(sk), &fl4, sk);
  236. }
  237. return rt;
  238. }
  239. extern void rt_bind_peer(struct rtable *rt, int create);
  240. static inline struct inet_peer *rt_get_peer(struct rtable *rt)
  241. {
  242. if (rt->peer)
  243. return rt->peer;
  244. rt_bind_peer(rt, 0);
  245. return rt->peer;
  246. }
  247. static inline int inet_iif(const struct sk_buff *skb)
  248. {
  249. return skb_rtable(skb)->rt_iif;
  250. }
  251. extern int sysctl_ip_default_ttl;
  252. static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
  253. {
  254. int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
  255. if (hoplimit == 0)
  256. hoplimit = sysctl_ip_default_ttl;
  257. return hoplimit;
  258. }
  259. #endif /* _ROUTE_H */