1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_INETDEVICE_H
3#define _LINUX_INETDEVICE_H
4
5#ifdef __KERNEL__
6
7#include <linux/bitmap.h>
8#include <linux/if.h>
9#include <linux/ip.h>
10#include <linux/netdevice.h>
11#include <linux/rcupdate.h>
12#include <linux/timer.h>
13#include <linux/sysctl.h>
14#include <linux/rtnetlink.h>
15#include <linux/refcount.h>
16
17struct ipv4_devconf {
18	void	*sysctl;
19	int	data[IPV4_DEVCONF_MAX];
20	DECLARE_BITMAP(state, IPV4_DEVCONF_MAX);
21};
22
23#define MC_HASH_SZ_LOG 9
24
25struct in_device {
26	struct net_device	*dev;
27	netdevice_tracker	dev_tracker;
28
29	refcount_t		refcnt;
30	int			dead;
31	struct in_ifaddr	__rcu *ifa_list;/* IP ifaddr chain		*/
32
33	struct ip_mc_list __rcu	*mc_list;	/* IP multicast filter chain    */
34	struct ip_mc_list __rcu	* __rcu *mc_hash;
35
36	int			mc_count;	/* Number of installed mcasts	*/
37	spinlock_t		mc_tomb_lock;
38	struct ip_mc_list	*mc_tomb;
39	unsigned long		mr_v1_seen;
40	unsigned long		mr_v2_seen;
41	unsigned long		mr_maxdelay;
42	unsigned long		mr_qi;		/* Query Interval */
43	unsigned long		mr_qri;		/* Query Response Interval */
44	unsigned char		mr_qrv;		/* Query Robustness Variable */
45	unsigned char		mr_gq_running;
46	u32			mr_ifc_count;
47	struct timer_list	mr_gq_timer;	/* general query timer */
48	struct timer_list	mr_ifc_timer;	/* interface change timer */
49
50	struct neigh_parms	*arp_parms;
51	struct ipv4_devconf	cnf;
52	struct rcu_head		rcu_head;
53};
54
55#define IPV4_DEVCONF(cnf, attr) ((cnf).data[IPV4_DEVCONF_ ## attr - 1])
56#define IPV4_DEVCONF_RO(cnf, attr) READ_ONCE(IPV4_DEVCONF(cnf, attr))
57#define IPV4_DEVCONF_ALL(net, attr) \
58	IPV4_DEVCONF((*(net)->ipv4.devconf_all), attr)
59#define IPV4_DEVCONF_ALL_RO(net, attr) READ_ONCE(IPV4_DEVCONF_ALL(net, attr))
60
61static inline int ipv4_devconf_get(const struct in_device *in_dev, int index)
62{
63	index--;
64	return READ_ONCE(in_dev->cnf.data[index]);
65}
66
67static inline void ipv4_devconf_set(struct in_device *in_dev, int index,
68				    int val)
69{
70	index--;
71	set_bit(index, in_dev->cnf.state);
72	WRITE_ONCE(in_dev->cnf.data[index], val);
73}
74
75static inline void ipv4_devconf_setall(struct in_device *in_dev)
76{
77	bitmap_fill(in_dev->cnf.state, IPV4_DEVCONF_MAX);
78}
79
80#define IN_DEV_CONF_GET(in_dev, attr) \
81	ipv4_devconf_get((in_dev), IPV4_DEVCONF_ ## attr)
82#define IN_DEV_CONF_SET(in_dev, attr, val) \
83	ipv4_devconf_set((in_dev), IPV4_DEVCONF_ ## attr, (val))
84
85#define IN_DEV_ANDCONF(in_dev, attr) \
86	(IPV4_DEVCONF_ALL_RO(dev_net(in_dev->dev), attr) && \
87	 IN_DEV_CONF_GET((in_dev), attr))
88
89#define IN_DEV_NET_ORCONF(in_dev, net, attr) \
90	(IPV4_DEVCONF_ALL_RO(net, attr) || \
91	 IN_DEV_CONF_GET((in_dev), attr))
92
93#define IN_DEV_ORCONF(in_dev, attr) \
94	IN_DEV_NET_ORCONF(in_dev, dev_net(in_dev->dev), attr)
95
96#define IN_DEV_MAXCONF(in_dev, attr) \
97	(max(IPV4_DEVCONF_ALL_RO(dev_net(in_dev->dev), attr), \
98	     IN_DEV_CONF_GET((in_dev), attr)))
99
100#define IN_DEV_FORWARD(in_dev)		IN_DEV_CONF_GET((in_dev), FORWARDING)
101#define IN_DEV_MFORWARD(in_dev)		IN_DEV_ANDCONF((in_dev), MC_FORWARDING)
102#define IN_DEV_BFORWARD(in_dev)		IN_DEV_ANDCONF((in_dev), BC_FORWARDING)
103#define IN_DEV_RPFILTER(in_dev)		IN_DEV_MAXCONF((in_dev), RP_FILTER)
104#define IN_DEV_SRC_VMARK(in_dev)    	IN_DEV_ORCONF((in_dev), SRC_VMARK)
105#define IN_DEV_SOURCE_ROUTE(in_dev)	IN_DEV_ANDCONF((in_dev), \
106						       ACCEPT_SOURCE_ROUTE)
107#define IN_DEV_ACCEPT_LOCAL(in_dev)	IN_DEV_ORCONF((in_dev), ACCEPT_LOCAL)
108#define IN_DEV_BOOTP_RELAY(in_dev)	IN_DEV_ANDCONF((in_dev), BOOTP_RELAY)
109
110#define IN_DEV_LOG_MARTIANS(in_dev)	IN_DEV_ORCONF((in_dev), LOG_MARTIANS)
111#define IN_DEV_PROXY_ARP(in_dev)	IN_DEV_ORCONF((in_dev), PROXY_ARP)
112#define IN_DEV_PROXY_ARP_PVLAN(in_dev)	IN_DEV_ORCONF((in_dev), PROXY_ARP_PVLAN)
113#define IN_DEV_SHARED_MEDIA(in_dev)	IN_DEV_ORCONF((in_dev), SHARED_MEDIA)
114#define IN_DEV_TX_REDIRECTS(in_dev)	IN_DEV_ORCONF((in_dev), SEND_REDIRECTS)
115#define IN_DEV_SEC_REDIRECTS(in_dev)	IN_DEV_ORCONF((in_dev), \
116						      SECURE_REDIRECTS)
117#define IN_DEV_IDTAG(in_dev)		IN_DEV_CONF_GET(in_dev, TAG)
118#define IN_DEV_MEDIUM_ID(in_dev)	IN_DEV_CONF_GET(in_dev, MEDIUM_ID)
119#define IN_DEV_PROMOTE_SECONDARIES(in_dev) \
120					IN_DEV_ORCONF((in_dev), \
121						      PROMOTE_SECONDARIES)
122#define IN_DEV_ROUTE_LOCALNET(in_dev)	IN_DEV_ORCONF(in_dev, ROUTE_LOCALNET)
123#define IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)	\
124	IN_DEV_NET_ORCONF(in_dev, net, ROUTE_LOCALNET)
125
126#define IN_DEV_RX_REDIRECTS(in_dev) \
127	((IN_DEV_FORWARD(in_dev) && \
128	  IN_DEV_ANDCONF((in_dev), ACCEPT_REDIRECTS)) \
129	 || (!IN_DEV_FORWARD(in_dev) && \
130	  IN_DEV_ORCONF((in_dev), ACCEPT_REDIRECTS)))
131
132#define IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) \
133	IN_DEV_ORCONF((in_dev), IGNORE_ROUTES_WITH_LINKDOWN)
134
135#define IN_DEV_ARPFILTER(in_dev)	IN_DEV_ORCONF((in_dev), ARPFILTER)
136#define IN_DEV_ARP_ACCEPT(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_ACCEPT)
137#define IN_DEV_ARP_ANNOUNCE(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_ANNOUNCE)
138#define IN_DEV_ARP_IGNORE(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_IGNORE)
139#define IN_DEV_ARP_NOTIFY(in_dev)	IN_DEV_MAXCONF((in_dev), ARP_NOTIFY)
140#define IN_DEV_ARP_EVICT_NOCARRIER(in_dev) IN_DEV_ANDCONF((in_dev), \
141							  ARP_EVICT_NOCARRIER)
142
143struct in_ifaddr {
144	struct hlist_node	hash;
145	struct in_ifaddr	__rcu *ifa_next;
146	struct in_device	*ifa_dev;
147	struct rcu_head		rcu_head;
148	__be32			ifa_local;
149	__be32			ifa_address;
150	__be32			ifa_mask;
151	__u32			ifa_rt_priority;
152	__be32			ifa_broadcast;
153	unsigned char		ifa_scope;
154	unsigned char		ifa_prefixlen;
155	unsigned char		ifa_proto;
156	__u32			ifa_flags;
157	char			ifa_label[IFNAMSIZ];
158
159	/* In seconds, relative to tstamp. Expiry is at tstamp + HZ * lft. */
160	__u32			ifa_valid_lft;
161	__u32			ifa_preferred_lft;
162	unsigned long		ifa_cstamp; /* created timestamp */
163	unsigned long		ifa_tstamp; /* updated timestamp */
164};
165
166struct in_validator_info {
167	__be32			ivi_addr;
168	struct in_device	*ivi_dev;
169	struct netlink_ext_ack	*extack;
170};
171
172int register_inetaddr_notifier(struct notifier_block *nb);
173int unregister_inetaddr_notifier(struct notifier_block *nb);
174int register_inetaddr_validator_notifier(struct notifier_block *nb);
175int unregister_inetaddr_validator_notifier(struct notifier_block *nb);
176
177void inet_netconf_notify_devconf(struct net *net, int event, int type,
178				 int ifindex, struct ipv4_devconf *devconf);
179
180struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref);
181static inline struct net_device *ip_dev_find(struct net *net, __be32 addr)
182{
183	return __ip_dev_find(net, addr, true);
184}
185
186int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b);
187int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *);
188#ifdef CONFIG_INET
189int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size);
190#else
191static inline int inet_gifconf(struct net_device *dev, char __user *buf,
192			       int len, int size)
193{
194	return 0;
195}
196#endif
197void devinet_init(void);
198struct in_device *inetdev_by_index(struct net *, int);
199__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope);
200__be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, __be32 dst,
201			 __be32 local, int scope);
202struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
203				    __be32 mask);
204struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr);
205static inline bool inet_ifa_match(__be32 addr, const struct in_ifaddr *ifa)
206{
207	return !((addr^ifa->ifa_address)&ifa->ifa_mask);
208}
209
210/*
211 *	Check if a mask is acceptable.
212 */
213
214static __inline__ bool bad_mask(__be32 mask, __be32 addr)
215{
216	__u32 hmask;
217	if (addr & (mask = ~mask))
218		return true;
219	hmask = ntohl(mask);
220	if (hmask & (hmask+1))
221		return true;
222	return false;
223}
224
225#define in_dev_for_each_ifa_rtnl(ifa, in_dev)			\
226	for (ifa = rtnl_dereference((in_dev)->ifa_list); ifa;	\
227	     ifa = rtnl_dereference(ifa->ifa_next))
228
229#define in_dev_for_each_ifa_rcu(ifa, in_dev)			\
230	for (ifa = rcu_dereference((in_dev)->ifa_list); ifa;	\
231	     ifa = rcu_dereference(ifa->ifa_next))
232
233static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
234{
235	return rcu_dereference(dev->ip_ptr);
236}
237
238static inline struct in_device *in_dev_get(const struct net_device *dev)
239{
240	struct in_device *in_dev;
241
242	rcu_read_lock();
243	in_dev = __in_dev_get_rcu(dev);
244	if (in_dev)
245		refcount_inc(&in_dev->refcnt);
246	rcu_read_unlock();
247	return in_dev;
248}
249
250static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
251{
252	return rtnl_dereference(dev->ip_ptr);
253}
254
255/* called with rcu_read_lock or rtnl held */
256static inline bool ip_ignore_linkdown(const struct net_device *dev)
257{
258	struct in_device *in_dev;
259	bool rc = false;
260
261	in_dev = rcu_dereference_rtnl(dev->ip_ptr);
262	if (in_dev &&
263	    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
264		rc = true;
265
266	return rc;
267}
268
269static inline struct neigh_parms *__in_dev_arp_parms_get_rcu(const struct net_device *dev)
270{
271	struct in_device *in_dev = __in_dev_get_rcu(dev);
272
273	return in_dev ? in_dev->arp_parms : NULL;
274}
275
276void in_dev_finish_destroy(struct in_device *idev);
277
278static inline void in_dev_put(struct in_device *idev)
279{
280	if (refcount_dec_and_test(&idev->refcnt))
281		in_dev_finish_destroy(idev);
282}
283
284#define __in_dev_put(idev)  refcount_dec(&(idev)->refcnt)
285#define in_dev_hold(idev)   refcount_inc(&(idev)->refcnt)
286
287#endif /* __KERNEL__ */
288
289static __inline__ __be32 inet_make_mask(int logmask)
290{
291	if (logmask)
292		return htonl(~((1U<<(32-logmask))-1));
293	return 0;
294}
295
296static __inline__ int inet_mask_len(__be32 mask)
297{
298	__u32 hmask = ntohl(mask);
299	if (!hmask)
300		return 0;
301	return 32 - ffz(~hmask);
302}
303
304
305#endif /* _LINUX_INETDEVICE_H */
306