dst.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * net/dst.h Protocol independent destination cache definitions.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. */
  8. #ifndef _NET_DST_H
  9. #define _NET_DST_H
  10. #include <net/dst_ops.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/bug.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/refcount.h>
  17. #include <linux/rcuref.h>
  18. #include <net/neighbour.h>
  19. #include <asm/processor.h>
  20. #include <linux/indirect_call_wrapper.h>
  21. struct sk_buff;
  22. struct dst_entry {
  23. struct net_device *dev;
  24. struct dst_ops *ops;
  25. unsigned long _metrics;
  26. unsigned long expires;
  27. #ifdef CONFIG_XFRM
  28. struct xfrm_state *xfrm;
  29. #else
  30. void *__pad1;
  31. #endif
  32. int (*input)(struct sk_buff *);
  33. int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
  34. unsigned short flags;
  35. #define DST_NOXFRM 0x0002
  36. #define DST_NOPOLICY 0x0004
  37. #define DST_NOCOUNT 0x0008
  38. #define DST_FAKE_RTABLE 0x0010
  39. #define DST_XFRM_TUNNEL 0x0020
  40. #define DST_XFRM_QUEUE 0x0040
  41. #define DST_METADATA 0x0080
  42. /* A non-zero value of dst->obsolete forces by-hand validation
  43. * of the route entry. Positive values are set by the generic
  44. * dst layer to indicate that the entry has been forcefully
  45. * destroyed.
  46. *
  47. * Negative values are used by the implementation layer code to
  48. * force invocation of the dst_ops->check() method.
  49. */
  50. short obsolete;
  51. #define DST_OBSOLETE_NONE 0
  52. #define DST_OBSOLETE_DEAD 2
  53. #define DST_OBSOLETE_FORCE_CHK -1
  54. #define DST_OBSOLETE_KILL -2
  55. unsigned short header_len; /* more space at head required */
  56. unsigned short trailer_len; /* space to reserve at tail */
  57. /*
  58. * __rcuref wants to be on a different cache line from
  59. * input/output/ops or performance tanks badly
  60. */
  61. #ifdef CONFIG_64BIT
  62. rcuref_t __rcuref; /* 64-bit offset 64 */
  63. #endif
  64. int __use;
  65. unsigned long lastuse;
  66. struct rcu_head rcu_head;
  67. short error;
  68. short __pad;
  69. __u32 tclassid;
  70. #ifndef CONFIG_64BIT
  71. struct lwtunnel_state *lwtstate;
  72. rcuref_t __rcuref; /* 32-bit offset 64 */
  73. #endif
  74. netdevice_tracker dev_tracker;
  75. /*
  76. * Used by rtable and rt6_info. Moves lwtstate into the next cache
  77. * line on 64bit so that lwtstate does not cause false sharing with
  78. * __rcuref under contention of __rcuref. This also puts the
  79. * frequently accessed members of rtable and rt6_info out of the
  80. * __rcuref cache line.
  81. */
  82. struct list_head rt_uncached;
  83. struct uncached_list *rt_uncached_list;
  84. #ifdef CONFIG_64BIT
  85. struct lwtunnel_state *lwtstate;
  86. #endif
  87. };
  88. struct dst_metrics {
  89. u32 metrics[RTAX_MAX];
  90. refcount_t refcnt;
  91. } __aligned(4); /* Low pointer bits contain DST_METRICS_FLAGS */
  92. extern const struct dst_metrics dst_default_metrics;
  93. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
  94. #define DST_METRICS_READ_ONLY 0x1UL
  95. #define DST_METRICS_REFCOUNTED 0x2UL
  96. #define DST_METRICS_FLAGS 0x3UL
  97. #define __DST_METRICS_PTR(Y) \
  98. ((u32 *)((Y) & ~DST_METRICS_FLAGS))
  99. #define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
  100. static inline bool dst_metrics_read_only(const struct dst_entry *dst)
  101. {
  102. return dst->_metrics & DST_METRICS_READ_ONLY;
  103. }
  104. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
  105. static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
  106. {
  107. unsigned long val = dst->_metrics;
  108. if (!(val & DST_METRICS_READ_ONLY))
  109. __dst_destroy_metrics_generic(dst, val);
  110. }
  111. static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
  112. {
  113. unsigned long p = dst->_metrics;
  114. BUG_ON(!p);
  115. if (p & DST_METRICS_READ_ONLY)
  116. return dst->ops->cow_metrics(dst, p);
  117. return __DST_METRICS_PTR(p);
  118. }
  119. /* This may only be invoked before the entry has reached global
  120. * visibility.
  121. */
  122. static inline void dst_init_metrics(struct dst_entry *dst,
  123. const u32 *src_metrics,
  124. bool read_only)
  125. {
  126. dst->_metrics = ((unsigned long) src_metrics) |
  127. (read_only ? DST_METRICS_READ_ONLY : 0);
  128. }
  129. static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
  130. {
  131. u32 *dst_metrics = dst_metrics_write_ptr(dest);
  132. if (dst_metrics) {
  133. u32 *src_metrics = DST_METRICS_PTR(src);
  134. memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
  135. }
  136. }
  137. static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
  138. {
  139. return DST_METRICS_PTR(dst);
  140. }
  141. static inline u32
  142. dst_metric_raw(const struct dst_entry *dst, const int metric)
  143. {
  144. u32 *p = DST_METRICS_PTR(dst);
  145. return p[metric-1];
  146. }
  147. static inline u32
  148. dst_metric(const struct dst_entry *dst, const int metric)
  149. {
  150. WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
  151. metric == RTAX_ADVMSS ||
  152. metric == RTAX_MTU);
  153. return dst_metric_raw(dst, metric);
  154. }
  155. static inline u32
  156. dst_metric_advmss(const struct dst_entry *dst)
  157. {
  158. u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
  159. if (!advmss)
  160. advmss = dst->ops->default_advmss(dst);
  161. return advmss;
  162. }
  163. static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
  164. {
  165. u32 *p = dst_metrics_write_ptr(dst);
  166. if (p)
  167. p[metric-1] = val;
  168. }
  169. /* Kernel-internal feature bits that are unallocated in user space. */
  170. #define DST_FEATURE_ECN_CA (1U << 31)
  171. #define DST_FEATURE_MASK (DST_FEATURE_ECN_CA)
  172. #define DST_FEATURE_ECN_MASK (DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
  173. static inline u32
  174. dst_feature(const struct dst_entry *dst, u32 feature)
  175. {
  176. return dst_metric(dst, RTAX_FEATURES) & feature;
  177. }
  178. INDIRECT_CALLABLE_DECLARE(unsigned int ip6_mtu(const struct dst_entry *));
  179. INDIRECT_CALLABLE_DECLARE(unsigned int ipv4_mtu(const struct dst_entry *));
  180. static inline u32 dst_mtu(const struct dst_entry *dst)
  181. {
  182. return INDIRECT_CALL_INET(dst->ops->mtu, ip6_mtu, ipv4_mtu, dst);
  183. }
  184. /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
  185. static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
  186. {
  187. return msecs_to_jiffies(dst_metric(dst, metric));
  188. }
  189. static inline int
  190. dst_metric_locked(const struct dst_entry *dst, int metric)
  191. {
  192. return dst_metric(dst, RTAX_LOCK) & (1 << metric);
  193. }
  194. static inline void dst_hold(struct dst_entry *dst)
  195. {
  196. /*
  197. * If your kernel compilation stops here, please check
  198. * the placement of __rcuref in struct dst_entry
  199. */
  200. BUILD_BUG_ON(offsetof(struct dst_entry, __rcuref) & 63);
  201. WARN_ON(!rcuref_get(&dst->__rcuref));
  202. }
  203. static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
  204. {
  205. if (unlikely(time != dst->lastuse)) {
  206. dst->__use++;
  207. dst->lastuse = time;
  208. }
  209. }
  210. static inline struct dst_entry *dst_clone(struct dst_entry *dst)
  211. {
  212. if (dst)
  213. dst_hold(dst);
  214. return dst;
  215. }
  216. void dst_release(struct dst_entry *dst);
  217. void dst_release_immediate(struct dst_entry *dst);
  218. static inline void refdst_drop(unsigned long refdst)
  219. {
  220. if (!(refdst & SKB_DST_NOREF))
  221. dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
  222. }
  223. /**
  224. * skb_dst_drop - drops skb dst
  225. * @skb: buffer
  226. *
  227. * Drops dst reference count if a reference was taken.
  228. */
  229. static inline void skb_dst_drop(struct sk_buff *skb)
  230. {
  231. if (skb->_skb_refdst) {
  232. refdst_drop(skb->_skb_refdst);
  233. skb->_skb_refdst = 0UL;
  234. }
  235. }
  236. static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
  237. {
  238. nskb->slow_gro |= !!refdst;
  239. nskb->_skb_refdst = refdst;
  240. if (!(nskb->_skb_refdst & SKB_DST_NOREF))
  241. dst_clone(skb_dst(nskb));
  242. }
  243. static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
  244. {
  245. __skb_dst_copy(nskb, oskb->_skb_refdst);
  246. }
  247. /**
  248. * dst_hold_safe - Take a reference on a dst if possible
  249. * @dst: pointer to dst entry
  250. *
  251. * This helper returns false if it could not safely
  252. * take a reference on a dst.
  253. */
  254. static inline bool dst_hold_safe(struct dst_entry *dst)
  255. {
  256. return rcuref_get(&dst->__rcuref);
  257. }
  258. /**
  259. * skb_dst_force - makes sure skb dst is refcounted
  260. * @skb: buffer
  261. *
  262. * If dst is not yet refcounted and not destroyed, grab a ref on it.
  263. * Returns true if dst is refcounted.
  264. */
  265. static inline bool skb_dst_force(struct sk_buff *skb)
  266. {
  267. if (skb_dst_is_noref(skb)) {
  268. struct dst_entry *dst = skb_dst(skb);
  269. WARN_ON(!rcu_read_lock_held());
  270. if (!dst_hold_safe(dst))
  271. dst = NULL;
  272. skb->_skb_refdst = (unsigned long)dst;
  273. skb->slow_gro |= !!dst;
  274. }
  275. return skb->_skb_refdst != 0UL;
  276. }
  277. /**
  278. * __skb_tunnel_rx - prepare skb for rx reinsert
  279. * @skb: buffer
  280. * @dev: tunnel device
  281. * @net: netns for packet i/o
  282. *
  283. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  284. * so make some cleanups. (no accounting done)
  285. */
  286. static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  287. struct net *net)
  288. {
  289. skb->dev = dev;
  290. /*
  291. * Clear hash so that we can recalculate the hash for the
  292. * encapsulated packet, unless we have already determine the hash
  293. * over the L4 4-tuple.
  294. */
  295. skb_clear_hash_if_not_l4(skb);
  296. skb_set_queue_mapping(skb, 0);
  297. skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
  298. }
  299. /**
  300. * skb_tunnel_rx - prepare skb for rx reinsert
  301. * @skb: buffer
  302. * @dev: tunnel device
  303. * @net: netns for packet i/o
  304. *
  305. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  306. * so make some cleanups, and perform accounting.
  307. * Note: this accounting is not SMP safe.
  308. */
  309. static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  310. struct net *net)
  311. {
  312. DEV_STATS_INC(dev, rx_packets);
  313. DEV_STATS_ADD(dev, rx_bytes, skb->len);
  314. __skb_tunnel_rx(skb, dev, net);
  315. }
  316. static inline u32 dst_tclassid(const struct sk_buff *skb)
  317. {
  318. #ifdef CONFIG_IP_ROUTE_CLASSID
  319. const struct dst_entry *dst;
  320. dst = skb_dst(skb);
  321. if (dst)
  322. return dst->tclassid;
  323. #endif
  324. return 0;
  325. }
  326. int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  327. static inline int dst_discard(struct sk_buff *skb)
  328. {
  329. return dst_discard_out(&init_net, skb->sk, skb);
  330. }
  331. void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
  332. int initial_obsolete, unsigned short flags);
  333. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  334. struct net_device *dev, int initial_obsolete,
  335. unsigned short flags);
  336. void dst_dev_put(struct dst_entry *dst);
  337. static inline void dst_confirm(struct dst_entry *dst)
  338. {
  339. }
  340. static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
  341. {
  342. struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
  343. return IS_ERR(n) ? NULL : n;
  344. }
  345. static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
  346. struct sk_buff *skb)
  347. {
  348. struct neighbour *n;
  349. if (WARN_ON_ONCE(!dst->ops->neigh_lookup))
  350. return NULL;
  351. n = dst->ops->neigh_lookup(dst, skb, NULL);
  352. return IS_ERR(n) ? NULL : n;
  353. }
  354. static inline void dst_confirm_neigh(const struct dst_entry *dst,
  355. const void *daddr)
  356. {
  357. if (dst->ops->confirm_neigh)
  358. dst->ops->confirm_neigh(dst, daddr);
  359. }
  360. static inline void dst_link_failure(struct sk_buff *skb)
  361. {
  362. struct dst_entry *dst = skb_dst(skb);
  363. if (dst && dst->ops && dst->ops->link_failure)
  364. dst->ops->link_failure(skb);
  365. }
  366. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  367. {
  368. unsigned long expires = jiffies + timeout;
  369. if (expires == 0)
  370. expires = 1;
  371. if (dst->expires == 0 || time_before(expires, dst->expires))
  372. dst->expires = expires;
  373. }
  374. static inline unsigned int dst_dev_overhead(struct dst_entry *dst,
  375. struct sk_buff *skb)
  376. {
  377. if (likely(dst))
  378. return LL_RESERVED_SPACE(dst->dev);
  379. return skb->mac_len;
  380. }
  381. INDIRECT_CALLABLE_DECLARE(int ip6_output(struct net *, struct sock *,
  382. struct sk_buff *));
  383. INDIRECT_CALLABLE_DECLARE(int ip_output(struct net *, struct sock *,
  384. struct sk_buff *));
  385. /* Output packet to network from transport. */
  386. static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  387. {
  388. return INDIRECT_CALL_INET(READ_ONCE(skb_dst(skb)->output),
  389. ip6_output, ip_output,
  390. net, sk, skb);
  391. }
  392. INDIRECT_CALLABLE_DECLARE(int ip6_input(struct sk_buff *));
  393. INDIRECT_CALLABLE_DECLARE(int ip_local_deliver(struct sk_buff *));
  394. /* Input packet from network to transport. */
  395. static inline int dst_input(struct sk_buff *skb)
  396. {
  397. return INDIRECT_CALL_INET(READ_ONCE(skb_dst(skb)->input),
  398. ip6_input, ip_local_deliver, skb);
  399. }
  400. INDIRECT_CALLABLE_DECLARE(struct dst_entry *ip6_dst_check(struct dst_entry *,
  401. u32));
  402. INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
  403. u32));
  404. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  405. {
  406. if (dst->obsolete)
  407. dst = INDIRECT_CALL_INET(dst->ops->check, ip6_dst_check,
  408. ipv4_dst_check, dst, cookie);
  409. return dst;
  410. }
  411. /* Flags for xfrm_lookup flags argument. */
  412. enum {
  413. XFRM_LOOKUP_ICMP = 1 << 0,
  414. XFRM_LOOKUP_QUEUE = 1 << 1,
  415. XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
  416. };
  417. struct flowi;
  418. #ifndef CONFIG_XFRM
  419. static inline struct dst_entry *xfrm_lookup(struct net *net,
  420. struct dst_entry *dst_orig,
  421. const struct flowi *fl,
  422. const struct sock *sk,
  423. int flags)
  424. {
  425. return dst_orig;
  426. }
  427. static inline struct dst_entry *
  428. xfrm_lookup_with_ifid(struct net *net, struct dst_entry *dst_orig,
  429. const struct flowi *fl, const struct sock *sk,
  430. int flags, u32 if_id)
  431. {
  432. return dst_orig;
  433. }
  434. static inline struct dst_entry *xfrm_lookup_route(struct net *net,
  435. struct dst_entry *dst_orig,
  436. const struct flowi *fl,
  437. const struct sock *sk,
  438. int flags)
  439. {
  440. return dst_orig;
  441. }
  442. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  443. {
  444. return NULL;
  445. }
  446. #else
  447. struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
  448. const struct flowi *fl, const struct sock *sk,
  449. int flags);
  450. struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
  451. struct dst_entry *dst_orig,
  452. const struct flowi *fl,
  453. const struct sock *sk, int flags,
  454. u32 if_id);
  455. struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
  456. const struct flowi *fl, const struct sock *sk,
  457. int flags);
  458. /* skb attached with this dst needs transformation if dst->xfrm is valid */
  459. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  460. {
  461. return dst->xfrm;
  462. }
  463. #endif
  464. static inline void skb_dst_update_pmtu(struct sk_buff *skb, u32 mtu)
  465. {
  466. struct dst_entry *dst = skb_dst(skb);
  467. if (dst && dst->ops->update_pmtu)
  468. dst->ops->update_pmtu(dst, NULL, skb, mtu, true);
  469. }
  470. /* update dst pmtu but not do neighbor confirm */
  471. static inline void skb_dst_update_pmtu_no_confirm(struct sk_buff *skb, u32 mtu)
  472. {
  473. struct dst_entry *dst = skb_dst(skb);
  474. if (dst && dst->ops->update_pmtu)
  475. dst->ops->update_pmtu(dst, NULL, skb, mtu, false);
  476. }
  477. static inline struct net_device *dst_dev(const struct dst_entry *dst)
  478. {
  479. return READ_ONCE(dst->dev);
  480. }
  481. static inline struct net_device *dst_dev_rcu(const struct dst_entry *dst)
  482. {
  483. /* In the future, use rcu_dereference(dst->dev) */
  484. WARN_ON_ONCE(!rcu_read_lock_held());
  485. return READ_ONCE(dst->dev);
  486. }
  487. static inline struct net_device *skb_dst_dev(const struct sk_buff *skb)
  488. {
  489. return dst_dev(skb_dst(skb));
  490. }
  491. static inline struct net_device *skb_dst_dev_rcu(const struct sk_buff *skb)
  492. {
  493. return dst_dev_rcu(skb_dst(skb));
  494. }
  495. static inline struct net *skb_dst_dev_net(const struct sk_buff *skb)
  496. {
  497. return dev_net(skb_dst_dev(skb));
  498. }
  499. static inline struct net *skb_dst_dev_net_rcu(const struct sk_buff *skb)
  500. {
  501. return dev_net_rcu(skb_dst_dev(skb));
  502. }
  503. struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie);
  504. void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
  505. struct sk_buff *skb, u32 mtu, bool confirm_neigh);
  506. void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
  507. struct sk_buff *skb);
  508. u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old);
  509. struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
  510. struct sk_buff *skb,
  511. const void *daddr);
  512. unsigned int dst_blackhole_mtu(const struct dst_entry *dst);
  513. #endif /* _NET_DST_H */