| 1 | /*	$OpenBSD: rtable.h,v 1.36 2025/07/15 09:55:49 dlg Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2014-2016 Martin Pieuchot |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef	_NET_RTABLE_H_ |
| 20 | #define	_NET_RTABLE_H_ |
| 21 | |
| 22 | #include <sys/rwlock.h> |
| 23 | |
| 24 | struct art; |
| 25 | |
| 26 | /* |
| 27 | * Locks used to protect struct members in this file: |
| 28 | *	I	immutable after creation |
| 29 | *	N	net lock |
| 30 | */ |
| 31 | |
| 32 | struct rtable { |
| 33 | 	struct rwlock		 r_lock; |
| 34 | 	struct art		*r_art;		/* [I] */ |
| 35 | 	unsigned int		 r_off;		/* [I] Offset of key in bytes */ |
| 36 | |
| 37 | 	struct sockaddr		*r_source;	/* [N] use optional src addr */ |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Newer routing table implementation based on ART (Allotment Routing |
| 42 | * Table). |
| 43 | */ |
| 44 | |
| 45 | #define	rt_key(rt)	((rt)->rt_dest) |
| 46 | #define	rt_plen(rt)	((rt)->rt_plen) |
| 47 | #define	RT_ROOT(rt)	(0) |
| 48 | |
| 49 | int		 rtable_satoplen(sa_family_t, const struct sockaddr *); |
| 50 | |
| 51 | void		 rtable_init(void); |
| 52 | int		 rtable_exists(unsigned int); |
| 53 | int		 rtable_empty(unsigned int); |
| 54 | int		 rtable_add(unsigned int); |
| 55 | unsigned int	 rtable_l2(unsigned int); |
| 56 | unsigned int	 rtable_loindex(unsigned int); |
| 57 | void		 rtable_l2set(unsigned int, unsigned int, unsigned int); |
| 58 | |
| 59 | int		 rtable_setsource(unsigned int, int, struct sockaddr *); |
| 60 | struct sockaddr *rtable_getsource(unsigned int, int); |
| 61 | void		 rtable_clearsource(unsigned int, struct sockaddr *); |
| 62 | struct rtentry	*rtable_lookup(unsigned int, const struct sockaddr *, |
| 63 | 		 const struct sockaddr *, const struct sockaddr *, uint8_t); |
| 64 | struct rtentry	*rtable_match(unsigned int, const struct sockaddr *, |
| 65 | 		 uint32_t *); |
| 66 | struct rtentry	*rtable_iterate(struct rtentry *); |
| 67 | int		 rtable_insert(unsigned int, struct sockaddr *, |
| 68 | 		 const struct sockaddr *, const struct sockaddr *, uint8_t, |
| 69 | 		 struct rtentry *); |
| 70 | int		 rtable_delete(unsigned int, const struct sockaddr *, |
| 71 | 		 const struct sockaddr *, struct rtentry *); |
| 72 | int		 rtable_walk(unsigned int, sa_family_t, struct rtentry **, |
| 73 | 		 int (*)(struct rtentry *, void *, unsigned int), void *); |
| 74 | int		 rtable_read(unsigned int, sa_family_t, |
| 75 | 		 int (*)(const struct rtentry *, void *, unsigned int), |
| 76 | 		 void *); |
| 77 | |
| 78 | int		 rtable_mpath_capable(unsigned int, sa_family_t); |
| 79 | int		 rtable_mpath_reprio(unsigned int, struct sockaddr *, int, |
| 80 | 		 uint8_t, struct rtentry *); |
| 81 | |
| 82 | #endif /* _NET_RTABLE_H_ */ |