0001
0002 #ifndef _LINUX_INETDEVICE_H
0003 #define _LINUX_INETDEVICE_H
0004
0005 #ifdef __KERNEL__
0006
0007 #include <linux/bitmap.h>
0008 #include <linux/if.h>
0009 #include <linux/ip.h>
0010 #include <linux/netdevice.h>
0011 #include <linux/rcupdate.h>
0012 #include <linux/timer.h>
0013 #include <linux/sysctl.h>
0014 #include <linux/rtnetlink.h>
0015 #include <linux/refcount.h>
0016
0017 struct ipv4_devconf {
0018 void *sysctl;
0019 int data[IPV4_DEVCONF_MAX];
0020 DECLARE_BITMAP(state, IPV4_DEVCONF_MAX);
0021 };
0022
0023 #define MC_HASH_SZ_LOG 9
0024
0025 struct in_device {
0026 struct net_device *dev;
0027 netdevice_tracker dev_tracker;
0028
0029 refcount_t refcnt;
0030 int dead;
0031 struct in_ifaddr __rcu *ifa_list;
0032
0033 struct ip_mc_list __rcu *mc_list;
0034 struct ip_mc_list __rcu * __rcu *mc_hash;
0035
0036 int mc_count;
0037 spinlock_t mc_tomb_lock;
0038 struct ip_mc_list *mc_tomb;
0039 unsigned long mr_v1_seen;
0040 unsigned long mr_v2_seen;
0041 unsigned long mr_maxdelay;
0042 unsigned long mr_qi;
0043 unsigned long mr_qri;
0044 unsigned char mr_qrv;
0045 unsigned char mr_gq_running;
0046 u32 mr_ifc_count;
0047 struct timer_list mr_gq_timer;
0048 struct timer_list mr_ifc_timer;
0049
0050 struct neigh_parms *arp_parms;
0051 struct ipv4_devconf cnf;
0052 struct rcu_head rcu_head;
0053 };
0054
0055 #define IPV4_DEVCONF(cnf, attr) ((cnf).data[IPV4_DEVCONF_ ## attr - 1])
0056 #define IPV4_DEVCONF_ALL(net, attr) \
0057 IPV4_DEVCONF((*(net)->ipv4.devconf_all), attr)
0058
0059 static inline int ipv4_devconf_get(struct in_device *in_dev, int index)
0060 {
0061 index--;
0062 return in_dev->cnf.data[index];
0063 }
0064
0065 static inline void ipv4_devconf_set(struct in_device *in_dev, int index,
0066 int val)
0067 {
0068 index--;
0069 set_bit(index, in_dev->cnf.state);
0070 in_dev->cnf.data[index] = val;
0071 }
0072
0073 static inline void ipv4_devconf_setall(struct in_device *in_dev)
0074 {
0075 bitmap_fill(in_dev->cnf.state, IPV4_DEVCONF_MAX);
0076 }
0077
0078 #define IN_DEV_CONF_GET(in_dev, attr) \
0079 ipv4_devconf_get((in_dev), IPV4_DEVCONF_ ## attr)
0080 #define IN_DEV_CONF_SET(in_dev, attr, val) \
0081 ipv4_devconf_set((in_dev), IPV4_DEVCONF_ ## attr, (val))
0082
0083 #define IN_DEV_ANDCONF(in_dev, attr) \
0084 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), attr) && \
0085 IN_DEV_CONF_GET((in_dev), attr))
0086
0087 #define IN_DEV_NET_ORCONF(in_dev, net, attr) \
0088 (IPV4_DEVCONF_ALL(net, attr) || \
0089 IN_DEV_CONF_GET((in_dev), attr))
0090
0091 #define IN_DEV_ORCONF(in_dev, attr) \
0092 IN_DEV_NET_ORCONF(in_dev, dev_net(in_dev->dev), attr)
0093
0094 #define IN_DEV_MAXCONF(in_dev, attr) \
0095 (max(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), attr), \
0096 IN_DEV_CONF_GET((in_dev), attr)))
0097
0098 #define IN_DEV_FORWARD(in_dev) IN_DEV_CONF_GET((in_dev), FORWARDING)
0099 #define IN_DEV_MFORWARD(in_dev) IN_DEV_ANDCONF((in_dev), MC_FORWARDING)
0100 #define IN_DEV_BFORWARD(in_dev) IN_DEV_ANDCONF((in_dev), BC_FORWARDING)
0101 #define IN_DEV_RPFILTER(in_dev) IN_DEV_MAXCONF((in_dev), RP_FILTER)
0102 #define IN_DEV_SRC_VMARK(in_dev) IN_DEV_ORCONF((in_dev), SRC_VMARK)
0103 #define IN_DEV_SOURCE_ROUTE(in_dev) IN_DEV_ANDCONF((in_dev), \
0104 ACCEPT_SOURCE_ROUTE)
0105 #define IN_DEV_ACCEPT_LOCAL(in_dev) IN_DEV_ORCONF((in_dev), ACCEPT_LOCAL)
0106 #define IN_DEV_BOOTP_RELAY(in_dev) IN_DEV_ANDCONF((in_dev), BOOTP_RELAY)
0107
0108 #define IN_DEV_LOG_MARTIANS(in_dev) IN_DEV_ORCONF((in_dev), LOG_MARTIANS)
0109 #define IN_DEV_PROXY_ARP(in_dev) IN_DEV_ORCONF((in_dev), PROXY_ARP)
0110 #define IN_DEV_PROXY_ARP_PVLAN(in_dev) IN_DEV_ORCONF((in_dev), PROXY_ARP_PVLAN)
0111 #define IN_DEV_SHARED_MEDIA(in_dev) IN_DEV_ORCONF((in_dev), SHARED_MEDIA)
0112 #define IN_DEV_TX_REDIRECTS(in_dev) IN_DEV_ORCONF((in_dev), SEND_REDIRECTS)
0113 #define IN_DEV_SEC_REDIRECTS(in_dev) IN_DEV_ORCONF((in_dev), \
0114 SECURE_REDIRECTS)
0115 #define IN_DEV_IDTAG(in_dev) IN_DEV_CONF_GET(in_dev, TAG)
0116 #define IN_DEV_MEDIUM_ID(in_dev) IN_DEV_CONF_GET(in_dev, MEDIUM_ID)
0117 #define IN_DEV_PROMOTE_SECONDARIES(in_dev) \
0118 IN_DEV_ORCONF((in_dev), \
0119 PROMOTE_SECONDARIES)
0120 #define IN_DEV_ROUTE_LOCALNET(in_dev) IN_DEV_ORCONF(in_dev, ROUTE_LOCALNET)
0121 #define IN_DEV_NET_ROUTE_LOCALNET(in_dev, net) \
0122 IN_DEV_NET_ORCONF(in_dev, net, ROUTE_LOCALNET)
0123
0124 #define IN_DEV_RX_REDIRECTS(in_dev) \
0125 ((IN_DEV_FORWARD(in_dev) && \
0126 IN_DEV_ANDCONF((in_dev), ACCEPT_REDIRECTS)) \
0127 || (!IN_DEV_FORWARD(in_dev) && \
0128 IN_DEV_ORCONF((in_dev), ACCEPT_REDIRECTS)))
0129
0130 #define IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) \
0131 IN_DEV_ORCONF((in_dev), IGNORE_ROUTES_WITH_LINKDOWN)
0132
0133 #define IN_DEV_ARPFILTER(in_dev) IN_DEV_ORCONF((in_dev), ARPFILTER)
0134 #define IN_DEV_ARP_ACCEPT(in_dev) IN_DEV_MAXCONF((in_dev), ARP_ACCEPT)
0135 #define IN_DEV_ARP_ANNOUNCE(in_dev) IN_DEV_MAXCONF((in_dev), ARP_ANNOUNCE)
0136 #define IN_DEV_ARP_IGNORE(in_dev) IN_DEV_MAXCONF((in_dev), ARP_IGNORE)
0137 #define IN_DEV_ARP_NOTIFY(in_dev) IN_DEV_MAXCONF((in_dev), ARP_NOTIFY)
0138 #define IN_DEV_ARP_EVICT_NOCARRIER(in_dev) IN_DEV_ANDCONF((in_dev), \
0139 ARP_EVICT_NOCARRIER)
0140
0141 struct in_ifaddr {
0142 struct hlist_node hash;
0143 struct in_ifaddr __rcu *ifa_next;
0144 struct in_device *ifa_dev;
0145 struct rcu_head rcu_head;
0146 __be32 ifa_local;
0147 __be32 ifa_address;
0148 __be32 ifa_mask;
0149 __u32 ifa_rt_priority;
0150 __be32 ifa_broadcast;
0151 unsigned char ifa_scope;
0152 unsigned char ifa_prefixlen;
0153 unsigned char ifa_proto;
0154 __u32 ifa_flags;
0155 char ifa_label[IFNAMSIZ];
0156
0157
0158 __u32 ifa_valid_lft;
0159 __u32 ifa_preferred_lft;
0160 unsigned long ifa_cstamp;
0161 unsigned long ifa_tstamp;
0162 };
0163
0164 struct in_validator_info {
0165 __be32 ivi_addr;
0166 struct in_device *ivi_dev;
0167 struct netlink_ext_ack *extack;
0168 };
0169
0170 int register_inetaddr_notifier(struct notifier_block *nb);
0171 int unregister_inetaddr_notifier(struct notifier_block *nb);
0172 int register_inetaddr_validator_notifier(struct notifier_block *nb);
0173 int unregister_inetaddr_validator_notifier(struct notifier_block *nb);
0174
0175 void inet_netconf_notify_devconf(struct net *net, int event, int type,
0176 int ifindex, struct ipv4_devconf *devconf);
0177
0178 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref);
0179 static inline struct net_device *ip_dev_find(struct net *net, __be32 addr)
0180 {
0181 return __ip_dev_find(net, addr, true);
0182 }
0183
0184 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b);
0185 int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *);
0186 #ifdef CONFIG_INET
0187 int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size);
0188 #else
0189 static inline int inet_gifconf(struct net_device *dev, char __user *buf,
0190 int len, int size)
0191 {
0192 return 0;
0193 }
0194 #endif
0195 void devinet_init(void);
0196 struct in_device *inetdev_by_index(struct net *, int);
0197 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope);
0198 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, __be32 dst,
0199 __be32 local, int scope);
0200 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
0201 __be32 mask);
0202 struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr);
0203 static inline bool inet_ifa_match(__be32 addr, const struct in_ifaddr *ifa)
0204 {
0205 return !((addr^ifa->ifa_address)&ifa->ifa_mask);
0206 }
0207
0208
0209
0210
0211
0212 static __inline__ bool bad_mask(__be32 mask, __be32 addr)
0213 {
0214 __u32 hmask;
0215 if (addr & (mask = ~mask))
0216 return true;
0217 hmask = ntohl(mask);
0218 if (hmask & (hmask+1))
0219 return true;
0220 return false;
0221 }
0222
0223 #define in_dev_for_each_ifa_rtnl(ifa, in_dev) \
0224 for (ifa = rtnl_dereference((in_dev)->ifa_list); ifa; \
0225 ifa = rtnl_dereference(ifa->ifa_next))
0226
0227 #define in_dev_for_each_ifa_rcu(ifa, in_dev) \
0228 for (ifa = rcu_dereference((in_dev)->ifa_list); ifa; \
0229 ifa = rcu_dereference(ifa->ifa_next))
0230
0231 static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
0232 {
0233 return rcu_dereference(dev->ip_ptr);
0234 }
0235
0236 static inline struct in_device *in_dev_get(const struct net_device *dev)
0237 {
0238 struct in_device *in_dev;
0239
0240 rcu_read_lock();
0241 in_dev = __in_dev_get_rcu(dev);
0242 if (in_dev)
0243 refcount_inc(&in_dev->refcnt);
0244 rcu_read_unlock();
0245 return in_dev;
0246 }
0247
0248 static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
0249 {
0250 return rtnl_dereference(dev->ip_ptr);
0251 }
0252
0253
0254 static inline bool ip_ignore_linkdown(const struct net_device *dev)
0255 {
0256 struct in_device *in_dev;
0257 bool rc = false;
0258
0259 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
0260 if (in_dev &&
0261 IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
0262 rc = true;
0263
0264 return rc;
0265 }
0266
0267 static inline struct neigh_parms *__in_dev_arp_parms_get_rcu(const struct net_device *dev)
0268 {
0269 struct in_device *in_dev = __in_dev_get_rcu(dev);
0270
0271 return in_dev ? in_dev->arp_parms : NULL;
0272 }
0273
0274 void in_dev_finish_destroy(struct in_device *idev);
0275
0276 static inline void in_dev_put(struct in_device *idev)
0277 {
0278 if (refcount_dec_and_test(&idev->refcnt))
0279 in_dev_finish_destroy(idev);
0280 }
0281
0282 #define __in_dev_put(idev) refcount_dec(&(idev)->refcnt)
0283 #define in_dev_hold(idev) refcount_inc(&(idev)->refcnt)
0284
0285 #endif
0286
0287 static __inline__ __be32 inet_make_mask(int logmask)
0288 {
0289 if (logmask)
0290 return htonl(~((1U<<(32-logmask))-1));
0291 return 0;
0292 }
0293
0294 static __inline__ int inet_mask_len(__be32 mask)
0295 {
0296 __u32 hmask = ntohl(mask);
0297 if (!hmask)
0298 return 0;
0299 return 32 - ffz(~hmask);
0300 }
0301
0302
0303 #endif