1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_INETDEVICE_H
31da177e4SLinus Torvalds #define _LINUX_INETDEVICE_H
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds #ifdef __KERNEL__
61da177e4SLinus Torvalds
731be3085SHerbert Xu #include <linux/bitmap.h>
81da177e4SLinus Torvalds #include <linux/if.h>
94a5a8aa6Sstephen hemminger #include <linux/ip.h>
101da177e4SLinus Torvalds #include <linux/netdevice.h>
111da177e4SLinus Torvalds #include <linux/rcupdate.h>
121da177e4SLinus Torvalds #include <linux/timer.h>
138bfe6d68SSatyam Sharma #include <linux/sysctl.h>
1495ae6b22SEric Dumazet #include <linux/rtnetlink.h>
157658b36fSReshetova, Elena #include <linux/refcount.h>
161da177e4SLinus Torvalds
17d94d9feeSEric Dumazet struct ipv4_devconf {
181da177e4SLinus Torvalds void *sysctl;
19ca7479ebSThomas Graf int data[IPV4_DEVCONF_MAX];
20ca7479ebSThomas Graf DECLARE_BITMAP(state, IPV4_DEVCONF_MAX);
211da177e4SLinus Torvalds };
221da177e4SLinus Torvalds
23e9897071SEric Dumazet #define MC_HASH_SZ_LOG 9
24e9897071SEric Dumazet
25d94d9feeSEric Dumazet struct in_device {
261da177e4SLinus Torvalds struct net_device *dev;
27c04438f5SEric Dumazet netdevice_tracker dev_tracker;
28c04438f5SEric Dumazet
297658b36fSReshetova, Elena refcount_t refcnt;
301da177e4SLinus Torvalds int dead;
312638eb8bSFlorian Westphal struct in_ifaddr __rcu *ifa_list;/* IP ifaddr chain */
32e9897071SEric Dumazet
331d7138deSEric Dumazet struct ip_mc_list __rcu *mc_list; /* IP multicast filter chain */
34e9897071SEric Dumazet struct ip_mc_list __rcu * __rcu *mc_hash;
35e9897071SEric Dumazet
36b8bae41eSRami Rosen int mc_count; /* Number of installed mcasts */
371da177e4SLinus Torvalds spinlock_t mc_tomb_lock;
381da177e4SLinus Torvalds struct ip_mc_list *mc_tomb;
391da177e4SLinus Torvalds unsigned long mr_v1_seen;
401da177e4SLinus Torvalds unsigned long mr_v2_seen;
411da177e4SLinus Torvalds unsigned long mr_maxdelay;
42966c37f2SHangbin Liu unsigned long mr_qi; /* Query Interval */
43966c37f2SHangbin Liu unsigned long mr_qri; /* Query Response Interval */
44966c37f2SHangbin Liu unsigned char mr_qrv; /* Query Robustness Variable */
451da177e4SLinus Torvalds unsigned char mr_gq_running;
46b69dd5b3SEric Dumazet u32 mr_ifc_count;
471da177e4SLinus Torvalds struct timer_list mr_gq_timer; /* general query timer */
481da177e4SLinus Torvalds struct timer_list mr_ifc_timer; /* interface change timer */
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds struct neigh_parms *arp_parms;
511da177e4SLinus Torvalds struct ipv4_devconf cnf;
521da177e4SLinus Torvalds struct rcu_head rcu_head;
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds
5502291680SEric W. Biederman #define IPV4_DEVCONF(cnf, attr) ((cnf).data[IPV4_DEVCONF_ ## attr - 1])
56586f1211SPavel Emelyanov #define IPV4_DEVCONF_ALL(net, attr) \
57586f1211SPavel Emelyanov IPV4_DEVCONF((*(net)->ipv4.devconf_all), attr)
581da177e4SLinus Torvalds
ipv4_devconf_get(struct in_device * in_dev,int index)5942f811b8SHerbert Xu static inline int ipv4_devconf_get(struct in_device *in_dev, int index)
6042f811b8SHerbert Xu {
6142f811b8SHerbert Xu index--;
6242f811b8SHerbert Xu return in_dev->cnf.data[index];
6342f811b8SHerbert Xu }
6442f811b8SHerbert Xu
ipv4_devconf_set(struct in_device * in_dev,int index,int val)6542f811b8SHerbert Xu static inline void ipv4_devconf_set(struct in_device *in_dev, int index,
6642f811b8SHerbert Xu int val)
6742f811b8SHerbert Xu {
6842f811b8SHerbert Xu index--;
6931be3085SHerbert Xu set_bit(index, in_dev->cnf.state);
7042f811b8SHerbert Xu in_dev->cnf.data[index] = val;
7142f811b8SHerbert Xu }
7242f811b8SHerbert Xu
ipv4_devconf_setall(struct in_device * in_dev)7371e27da9SHerbert Xu static inline void ipv4_devconf_setall(struct in_device *in_dev)
7471e27da9SHerbert Xu {
75ca7479ebSThomas Graf bitmap_fill(in_dev->cnf.state, IPV4_DEVCONF_MAX);
7671e27da9SHerbert Xu }
7771e27da9SHerbert Xu
7842f811b8SHerbert Xu #define IN_DEV_CONF_GET(in_dev, attr) \
7902291680SEric W. Biederman ipv4_devconf_get((in_dev), IPV4_DEVCONF_ ## attr)
8042f811b8SHerbert Xu #define IN_DEV_CONF_SET(in_dev, attr, val) \
8102291680SEric W. Biederman ipv4_devconf_set((in_dev), IPV4_DEVCONF_ ## attr, (val))
8242f811b8SHerbert Xu
8342f811b8SHerbert Xu #define IN_DEV_ANDCONF(in_dev, attr) \
84c346dca1SYOSHIFUJI Hideaki (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), attr) && \
85586f1211SPavel Emelyanov IN_DEV_CONF_GET((in_dev), attr))
869eb43e76SEric Dumazet
879eb43e76SEric Dumazet #define IN_DEV_NET_ORCONF(in_dev, net, attr) \
889eb43e76SEric Dumazet (IPV4_DEVCONF_ALL(net, attr) || \
89586f1211SPavel Emelyanov IN_DEV_CONF_GET((in_dev), attr))
909eb43e76SEric Dumazet
919eb43e76SEric Dumazet #define IN_DEV_ORCONF(in_dev, attr) \
929eb43e76SEric Dumazet IN_DEV_NET_ORCONF(in_dev, dev_net(in_dev->dev), attr)
939eb43e76SEric Dumazet
9442f811b8SHerbert Xu #define IN_DEV_MAXCONF(in_dev, attr) \
95c346dca1SYOSHIFUJI Hideaki (max(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), attr), \
96586f1211SPavel Emelyanov IN_DEV_CONF_GET((in_dev), attr)))
9742f811b8SHerbert Xu
9842f811b8SHerbert Xu #define IN_DEV_FORWARD(in_dev) IN_DEV_CONF_GET((in_dev), FORWARDING)
9901ecfe9bSPavel Emelyanov #define IN_DEV_MFORWARD(in_dev) IN_DEV_ANDCONF((in_dev), MC_FORWARDING)
1005cbf777cSXin Long #define IN_DEV_BFORWARD(in_dev) IN_DEV_ANDCONF((in_dev), BC_FORWARDING)
10127fed417SStephen Hemminger #define IN_DEV_RPFILTER(in_dev) IN_DEV_MAXCONF((in_dev), RP_FILTER)
10228f6aeeaSJamal Hadi Salim #define IN_DEV_SRC_VMARK(in_dev) IN_DEV_ORCONF((in_dev), SRC_VMARK)
10342f811b8SHerbert Xu #define IN_DEV_SOURCE_ROUTE(in_dev) IN_DEV_ANDCONF((in_dev), \
10442f811b8SHerbert Xu ACCEPT_SOURCE_ROUTE)
1058153a10cSPatrick McHardy #define IN_DEV_ACCEPT_LOCAL(in_dev) IN_DEV_ORCONF((in_dev), ACCEPT_LOCAL)
10642f811b8SHerbert Xu #define IN_DEV_BOOTP_RELAY(in_dev) IN_DEV_ANDCONF((in_dev), BOOTP_RELAY)
10742f811b8SHerbert Xu
10842f811b8SHerbert Xu #define IN_DEV_LOG_MARTIANS(in_dev) IN_DEV_ORCONF((in_dev), LOG_MARTIANS)
10942f811b8SHerbert Xu #define IN_DEV_PROXY_ARP(in_dev) IN_DEV_ORCONF((in_dev), PROXY_ARP)
1101af5318cSVincent Bernat #define IN_DEV_PROXY_ARP_PVLAN(in_dev) IN_DEV_ORCONF((in_dev), PROXY_ARP_PVLAN)
11142f811b8SHerbert Xu #define IN_DEV_SHARED_MEDIA(in_dev) IN_DEV_ORCONF((in_dev), SHARED_MEDIA)
11242f811b8SHerbert Xu #define IN_DEV_TX_REDIRECTS(in_dev) IN_DEV_ORCONF((in_dev), SEND_REDIRECTS)
11342f811b8SHerbert Xu #define IN_DEV_SEC_REDIRECTS(in_dev) IN_DEV_ORCONF((in_dev), \
11442f811b8SHerbert Xu SECURE_REDIRECTS)
11542f811b8SHerbert Xu #define IN_DEV_IDTAG(in_dev) IN_DEV_CONF_GET(in_dev, TAG)
11642f811b8SHerbert Xu #define IN_DEV_MEDIUM_ID(in_dev) IN_DEV_CONF_GET(in_dev, MEDIUM_ID)
11742f811b8SHerbert Xu #define IN_DEV_PROMOTE_SECONDARIES(in_dev) \
11842f811b8SHerbert Xu IN_DEV_ORCONF((in_dev), \
11942f811b8SHerbert Xu PROMOTE_SECONDARIES)
120d0daebc3SThomas Graf #define IN_DEV_ROUTE_LOCALNET(in_dev) IN_DEV_ORCONF(in_dev, ROUTE_LOCALNET)
1219eb43e76SEric Dumazet #define IN_DEV_NET_ROUTE_LOCALNET(in_dev, net) \
1229eb43e76SEric Dumazet IN_DEV_NET_ORCONF(in_dev, net, ROUTE_LOCALNET)
1231da177e4SLinus Torvalds
1241da177e4SLinus Torvalds #define IN_DEV_RX_REDIRECTS(in_dev) \
1251da177e4SLinus Torvalds ((IN_DEV_FORWARD(in_dev) && \
12642f811b8SHerbert Xu IN_DEV_ANDCONF((in_dev), ACCEPT_REDIRECTS)) \
1271da177e4SLinus Torvalds || (!IN_DEV_FORWARD(in_dev) && \
12842f811b8SHerbert Xu IN_DEV_ORCONF((in_dev), ACCEPT_REDIRECTS)))
1291da177e4SLinus Torvalds
1300eeb075fSAndy Gospodarek #define IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) \
131c0c5a60fSVincent Bernat IN_DEV_ORCONF((in_dev), IGNORE_ROUTES_WITH_LINKDOWN)
1320eeb075fSAndy Gospodarek
13342f811b8SHerbert Xu #define IN_DEV_ARPFILTER(in_dev) IN_DEV_ORCONF((in_dev), ARPFILTER)
134*e68c5dcfSJaehee Park #define IN_DEV_ARP_ACCEPT(in_dev) IN_DEV_MAXCONF((in_dev), ARP_ACCEPT)
13542f811b8SHerbert Xu #define IN_DEV_ARP_ANNOUNCE(in_dev) IN_DEV_MAXCONF((in_dev), ARP_ANNOUNCE)
13642f811b8SHerbert Xu #define IN_DEV_ARP_IGNORE(in_dev) IN_DEV_MAXCONF((in_dev), ARP_IGNORE)
137eefef1cfSStephen Hemminger #define IN_DEV_ARP_NOTIFY(in_dev) IN_DEV_MAXCONF((in_dev), ARP_NOTIFY)
138fcdb44d0SJames Prestwood #define IN_DEV_ARP_EVICT_NOCARRIER(in_dev) IN_DEV_ANDCONF((in_dev), \
139fcdb44d0SJames Prestwood ARP_EVICT_NOCARRIER)
1401da177e4SLinus Torvalds
141d94d9feeSEric Dumazet struct in_ifaddr {
142fd23c3b3SDavid S. Miller struct hlist_node hash;
1432638eb8bSFlorian Westphal struct in_ifaddr __rcu *ifa_next;
1441da177e4SLinus Torvalds struct in_device *ifa_dev;
1451da177e4SLinus Torvalds struct rcu_head rcu_head;
146a144ea4bSAl Viro __be32 ifa_local;
147a144ea4bSAl Viro __be32 ifa_address;
148a144ea4bSAl Viro __be32 ifa_mask;
149af4d768aSDavid Ahern __u32 ifa_rt_priority;
150a144ea4bSAl Viro __be32 ifa_broadcast;
1511da177e4SLinus Torvalds unsigned char ifa_scope;
1521da177e4SLinus Torvalds unsigned char ifa_prefixlen;
15347f0bd50SJacques de Laval unsigned char ifa_proto;
154ad6c8135SJiri Pirko __u32 ifa_flags;
1551da177e4SLinus Torvalds char ifa_label[IFNAMSIZ];
1565c766d64SJiri Pirko
1575c766d64SJiri Pirko /* In seconds, relative to tstamp. Expiry is at tstamp + HZ * lft. */
1585c766d64SJiri Pirko __u32 ifa_valid_lft;
1595c766d64SJiri Pirko __u32 ifa_preferred_lft;
1605c766d64SJiri Pirko unsigned long ifa_cstamp; /* created timestamp */
1615c766d64SJiri Pirko unsigned long ifa_tstamp; /* updated timestamp */
1621da177e4SLinus Torvalds };
1631da177e4SLinus Torvalds
1643ad7d246SKrister Johansen struct in_validator_info {
1653ad7d246SKrister Johansen __be32 ivi_addr;
1663ad7d246SKrister Johansen struct in_device *ivi_dev;
167de95e047SDavid Ahern struct netlink_ext_ack *extack;
1683ad7d246SKrister Johansen };
1693ad7d246SKrister Johansen
170f629d208SJoe Perches int register_inetaddr_notifier(struct notifier_block *nb);
171f629d208SJoe Perches int unregister_inetaddr_notifier(struct notifier_block *nb);
1723ad7d246SKrister Johansen int register_inetaddr_validator_notifier(struct notifier_block *nb);
1733ad7d246SKrister Johansen int unregister_inetaddr_validator_notifier(struct notifier_block *nb);
1741da177e4SLinus Torvalds
1753b022865SDavid Ahern void inet_netconf_notify_devconf(struct net *net, int event, int type,
1763b022865SDavid Ahern int ifindex, struct ipv4_devconf *devconf);
177d67b8c61SNicolas Dichtel
178f629d208SJoe Perches struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref);
ip_dev_find(struct net * net,__be32 addr)17982efee14SEric Dumazet static inline struct net_device *ip_dev_find(struct net *net, __be32 addr)
18082efee14SEric Dumazet {
18182efee14SEric Dumazet return __ip_dev_find(net, addr, true);
18282efee14SEric Dumazet }
18382efee14SEric Dumazet
184f629d208SJoe Perches int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b);
18503aef17bSAl Viro int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *);
186b0e99d03SArnd Bergmann #ifdef CONFIG_INET
187b0e99d03SArnd Bergmann int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size);
188b0e99d03SArnd Bergmann #else
inet_gifconf(struct net_device * dev,char __user * buf,int len,int size)189b0e99d03SArnd Bergmann static inline int inet_gifconf(struct net_device *dev, char __user *buf,
190b0e99d03SArnd Bergmann int len, int size)
191b0e99d03SArnd Bergmann {
192b0e99d03SArnd Bergmann return 0;
193b0e99d03SArnd Bergmann }
194b0e99d03SArnd Bergmann #endif
195f629d208SJoe Perches void devinet_init(void);
196f629d208SJoe Perches struct in_device *inetdev_by_index(struct net *, int);
197f629d208SJoe Perches __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope);
198b601fa19SNicolas Dichtel __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, __be32 dst,
199b601fa19SNicolas Dichtel __be32 local, int scope);
200f629d208SJoe Perches struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
201f629d208SJoe Perches __be32 mask);
2026e617de8SPaolo Abeni struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr);
inet_ifa_match(__be32 addr,const struct in_ifaddr * ifa)203ef11db33SFlorian Westphal static inline bool inet_ifa_match(__be32 addr, const struct in_ifaddr *ifa)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds return !((addr^ifa->ifa_address)&ifa->ifa_mask);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds /*
2091da177e4SLinus Torvalds * Check if a mask is acceptable.
2101da177e4SLinus Torvalds */
2111da177e4SLinus Torvalds
bad_mask(__be32 mask,__be32 addr)212f06cc7b2SYaowei Bai static __inline__ bool bad_mask(__be32 mask, __be32 addr)
2131da177e4SLinus Torvalds {
214714e85beSAl Viro __u32 hmask;
2151da177e4SLinus Torvalds if (addr & (mask = ~mask))
216f06cc7b2SYaowei Bai return true;
217714e85beSAl Viro hmask = ntohl(mask);
218714e85beSAl Viro if (hmask & (hmask+1))
219f06cc7b2SYaowei Bai return true;
220f06cc7b2SYaowei Bai return false;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds
223ef11db33SFlorian Westphal #define in_dev_for_each_ifa_rtnl(ifa, in_dev) \
2242638eb8bSFlorian Westphal for (ifa = rtnl_dereference((in_dev)->ifa_list); ifa; \
2252638eb8bSFlorian Westphal ifa = rtnl_dereference(ifa->ifa_next))
226ef11db33SFlorian Westphal
227ef11db33SFlorian Westphal #define in_dev_for_each_ifa_rcu(ifa, in_dev) \
2282638eb8bSFlorian Westphal for (ifa = rcu_dereference((in_dev)->ifa_list); ifa; \
2292638eb8bSFlorian Westphal ifa = rcu_dereference(ifa->ifa_next))
230ef11db33SFlorian Westphal
__in_dev_get_rcu(const struct net_device * dev)231e5ed6399SHerbert Xu static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
232e5ed6399SHerbert Xu {
23395ae6b22SEric Dumazet return rcu_dereference(dev->ip_ptr);
234e5ed6399SHerbert Xu }
235e5ed6399SHerbert Xu
in_dev_get(const struct net_device * dev)23695ae6b22SEric Dumazet static inline struct in_device *in_dev_get(const struct net_device *dev)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds struct in_device *in_dev;
2391da177e4SLinus Torvalds
2401da177e4SLinus Torvalds rcu_read_lock();
241e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev);
2421da177e4SLinus Torvalds if (in_dev)
2437658b36fSReshetova, Elena refcount_inc(&in_dev->refcnt);
2441da177e4SLinus Torvalds rcu_read_unlock();
2451da177e4SLinus Torvalds return in_dev;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds
__in_dev_get_rtnl(const struct net_device * dev)24895ae6b22SEric Dumazet static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
2491da177e4SLinus Torvalds {
25006a9701fSEric Dumazet return rtnl_dereference(dev->ip_ptr);
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds
253331c7a40SDavid Ahern /* called with rcu_read_lock or rtnl held */
ip_ignore_linkdown(const struct net_device * dev)254331c7a40SDavid Ahern static inline bool ip_ignore_linkdown(const struct net_device *dev)
255331c7a40SDavid Ahern {
256331c7a40SDavid Ahern struct in_device *in_dev;
257331c7a40SDavid Ahern bool rc = false;
258331c7a40SDavid Ahern
259331c7a40SDavid Ahern in_dev = rcu_dereference_rtnl(dev->ip_ptr);
260331c7a40SDavid Ahern if (in_dev &&
261331c7a40SDavid Ahern IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
262331c7a40SDavid Ahern rc = true;
263331c7a40SDavid Ahern
264331c7a40SDavid Ahern return rc;
265331c7a40SDavid Ahern }
266331c7a40SDavid Ahern
__in_dev_arp_parms_get_rcu(const struct net_device * dev)2671d4c8c29SJiri Pirko static inline struct neigh_parms *__in_dev_arp_parms_get_rcu(const struct net_device *dev)
2681d4c8c29SJiri Pirko {
2691d4c8c29SJiri Pirko struct in_device *in_dev = __in_dev_get_rcu(dev);
2701d4c8c29SJiri Pirko
2711d4c8c29SJiri Pirko return in_dev ? in_dev->arp_parms : NULL;
2721d4c8c29SJiri Pirko }
2731d4c8c29SJiri Pirko
274f629d208SJoe Perches void in_dev_finish_destroy(struct in_device *idev);
2751da177e4SLinus Torvalds
in_dev_put(struct in_device * idev)2761da177e4SLinus Torvalds static inline void in_dev_put(struct in_device *idev)
2771da177e4SLinus Torvalds {
2787658b36fSReshetova, Elena if (refcount_dec_and_test(&idev->refcnt))
2791da177e4SLinus Torvalds in_dev_finish_destroy(idev);
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds
2827658b36fSReshetova, Elena #define __in_dev_put(idev) refcount_dec(&(idev)->refcnt)
2837658b36fSReshetova, Elena #define in_dev_hold(idev) refcount_inc(&(idev)->refcnt)
2841da177e4SLinus Torvalds
2851da177e4SLinus Torvalds #endif /* __KERNEL__ */
2861da177e4SLinus Torvalds
inet_make_mask(int logmask)28760cad5daSAl Viro static __inline__ __be32 inet_make_mask(int logmask)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds if (logmask)
29084bc8868SVincent BENAYOUN return htonl(~((1U<<(32-logmask))-1));
2911da177e4SLinus Torvalds return 0;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds
inet_mask_len(__be32 mask)294714e85beSAl Viro static __inline__ int inet_mask_len(__be32 mask)
2951da177e4SLinus Torvalds {
296714e85beSAl Viro __u32 hmask = ntohl(mask);
297714e85beSAl Viro if (!hmask)
2981da177e4SLinus Torvalds return 0;
299714e85beSAl Viro return 32 - ffz(~hmask);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds
3021da177e4SLinus Torvalds
3031da177e4SLinus Torvalds #endif /* _LINUX_INETDEVICE_H */
304