dst.h 14 KB

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