dst.h 16 KB

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