dst.h 14 KB

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