dst.h 14 KB

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