blob: c0f7aec331c2031f3aa0f1c89db00a7b0b14b699 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_INETDEVICE_H
2#define _LINUX_INETDEVICE_H
3
4#ifdef __KERNEL__
5
6#include <linux/if.h>
7#include <linux/netdevice.h>
8#include <linux/rcupdate.h>
9#include <linux/timer.h>
10
11struct ipv4_devconf
12{
13 int accept_redirects;
14 int send_redirects;
15 int secure_redirects;
16 int shared_media;
17 int accept_source_route;
18 int rp_filter;
19 int proxy_arp;
20 int bootp_relay;
21 int log_martians;
22 int forwarding;
23 int mc_forwarding;
24 int tag;
25 int arp_filter;
26 int arp_announce;
27 int arp_ignore;
Neil Hormanabd596a2006-03-20 22:39:47 -080028 int arp_accept;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 int medium_id;
30 int no_xfrm;
31 int no_policy;
32 int force_igmp_version;
Harald Welte8f937c62005-05-29 20:23:46 -070033 int promote_secondaries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 void *sysctl;
35};
36
37extern struct ipv4_devconf ipv4_devconf;
38
39struct in_device
40{
41 struct net_device *dev;
42 atomic_t refcnt;
43 int dead;
44 struct in_ifaddr *ifa_list; /* IP ifaddr chain */
45 rwlock_t mc_list_lock;
46 struct ip_mc_list *mc_list; /* IP multicast filter chain */
47 spinlock_t mc_tomb_lock;
48 struct ip_mc_list *mc_tomb;
49 unsigned long mr_v1_seen;
50 unsigned long mr_v2_seen;
51 unsigned long mr_maxdelay;
52 unsigned char mr_qrv;
53 unsigned char mr_gq_running;
54 unsigned char mr_ifc_count;
55 struct timer_list mr_gq_timer; /* general query timer */
56 struct timer_list mr_ifc_timer; /* interface change timer */
57
58 struct neigh_parms *arp_parms;
59 struct ipv4_devconf cnf;
60 struct rcu_head rcu_head;
61};
62
63#define IN_DEV_FORWARD(in_dev) ((in_dev)->cnf.forwarding)
64#define IN_DEV_MFORWARD(in_dev) (ipv4_devconf.mc_forwarding && (in_dev)->cnf.mc_forwarding)
65#define IN_DEV_RPFILTER(in_dev) (ipv4_devconf.rp_filter && (in_dev)->cnf.rp_filter)
66#define IN_DEV_SOURCE_ROUTE(in_dev) (ipv4_devconf.accept_source_route && (in_dev)->cnf.accept_source_route)
67#define IN_DEV_BOOTP_RELAY(in_dev) (ipv4_devconf.bootp_relay && (in_dev)->cnf.bootp_relay)
68
69#define IN_DEV_LOG_MARTIANS(in_dev) (ipv4_devconf.log_martians || (in_dev)->cnf.log_martians)
70#define IN_DEV_PROXY_ARP(in_dev) (ipv4_devconf.proxy_arp || (in_dev)->cnf.proxy_arp)
71#define IN_DEV_SHARED_MEDIA(in_dev) (ipv4_devconf.shared_media || (in_dev)->cnf.shared_media)
72#define IN_DEV_TX_REDIRECTS(in_dev) (ipv4_devconf.send_redirects || (in_dev)->cnf.send_redirects)
73#define IN_DEV_SEC_REDIRECTS(in_dev) (ipv4_devconf.secure_redirects || (in_dev)->cnf.secure_redirects)
74#define IN_DEV_IDTAG(in_dev) ((in_dev)->cnf.tag)
75#define IN_DEV_MEDIUM_ID(in_dev) ((in_dev)->cnf.medium_id)
Harald Welte8f937c62005-05-29 20:23:46 -070076#define IN_DEV_PROMOTE_SECONDARIES(in_dev) (ipv4_devconf.promote_secondaries || (in_dev)->cnf.promote_secondaries)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define IN_DEV_RX_REDIRECTS(in_dev) \
79 ((IN_DEV_FORWARD(in_dev) && \
80 (ipv4_devconf.accept_redirects && (in_dev)->cnf.accept_redirects)) \
81 || (!IN_DEV_FORWARD(in_dev) && \
82 (ipv4_devconf.accept_redirects || (in_dev)->cnf.accept_redirects)))
83
84#define IN_DEV_ARPFILTER(in_dev) (ipv4_devconf.arp_filter || (in_dev)->cnf.arp_filter)
85#define IN_DEV_ARP_ANNOUNCE(in_dev) (max(ipv4_devconf.arp_announce, (in_dev)->cnf.arp_announce))
86#define IN_DEV_ARP_IGNORE(in_dev) (max(ipv4_devconf.arp_ignore, (in_dev)->cnf.arp_ignore))
87
88struct in_ifaddr
89{
90 struct in_ifaddr *ifa_next;
91 struct in_device *ifa_dev;
92 struct rcu_head rcu_head;
Al Viroa144ea42006-09-28 18:00:55 -070093 __be32 ifa_local;
94 __be32 ifa_address;
95 __be32 ifa_mask;
96 __be32 ifa_broadcast;
97 __be32 ifa_anycast;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned char ifa_scope;
99 unsigned char ifa_flags;
100 unsigned char ifa_prefixlen;
101 char ifa_label[IFNAMSIZ];
102};
103
104extern int register_inetaddr_notifier(struct notifier_block *nb);
105extern int unregister_inetaddr_notifier(struct notifier_block *nb);
106
Al Viro60cad5d2006-09-26 22:17:09 -0700107extern struct net_device *ip_dev_find(__be32 addr);
Al Viroff428d72006-09-26 22:13:35 -0700108extern int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109extern int devinet_ioctl(unsigned int cmd, void __user *);
110extern void devinet_init(void);
111extern struct in_device *inetdev_init(struct net_device *dev);
112extern struct in_device *inetdev_by_index(int);
Al Viroa61ced52006-09-26 21:27:54 -0700113extern __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope);
Al Viro60cad5d2006-09-26 22:17:09 -0700114extern __be32 inet_confirm_addr(const struct net_device *dev, __be32 dst, __be32 local, int scope);
115extern struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, __be32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116extern void inet_forward_change(void);
117
Al Viro60cad5d2006-09-26 22:17:09 -0700118static __inline__ int inet_ifa_match(__be32 addr, struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 return !((addr^ifa->ifa_address)&ifa->ifa_mask);
121}
122
123/*
124 * Check if a mask is acceptable.
125 */
126
Al Viro714e85b2006-11-14 20:51:49 -0800127static __inline__ int bad_mask(__be32 mask, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Al Viro714e85b2006-11-14 20:51:49 -0800129 __u32 hmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (addr & (mask = ~mask))
131 return 1;
Al Viro714e85b2006-11-14 20:51:49 -0800132 hmask = ntohl(mask);
133 if (hmask & (hmask+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 1;
135 return 0;
136}
137
138#define for_primary_ifa(in_dev) { struct in_ifaddr *ifa; \
139 for (ifa = (in_dev)->ifa_list; ifa && !(ifa->ifa_flags&IFA_F_SECONDARY); ifa = ifa->ifa_next)
140
141#define for_ifa(in_dev) { struct in_ifaddr *ifa; \
142 for (ifa = (in_dev)->ifa_list; ifa; ifa = ifa->ifa_next)
143
144
145#define endfor_ifa(in_dev) }
146
Herbert Xue5ed6392005-10-03 14:35:55 -0700147static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
148{
149 struct in_device *in_dev = dev->ip_ptr;
150 if (in_dev)
151 in_dev = rcu_dereference(in_dev);
152 return in_dev;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static __inline__ struct in_device *
156in_dev_get(const struct net_device *dev)
157{
158 struct in_device *in_dev;
159
160 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700161 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (in_dev)
163 atomic_inc(&in_dev->refcnt);
164 rcu_read_unlock();
165 return in_dev;
166}
167
168static __inline__ struct in_device *
Herbert Xue5ed6392005-10-03 14:35:55 -0700169__in_dev_get_rtnl(const struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 return (struct in_device*)dev->ip_ptr;
172}
173
174extern void in_dev_finish_destroy(struct in_device *idev);
175
176static inline void in_dev_put(struct in_device *idev)
177{
178 if (atomic_dec_and_test(&idev->refcnt))
179 in_dev_finish_destroy(idev);
180}
181
182#define __in_dev_put(idev) atomic_dec(&(idev)->refcnt)
183#define in_dev_hold(idev) atomic_inc(&(idev)->refcnt)
184
185#endif /* __KERNEL__ */
186
Al Viro60cad5d2006-09-26 22:17:09 -0700187static __inline__ __be32 inet_make_mask(int logmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 if (logmask)
190 return htonl(~((1<<(32-logmask))-1));
191 return 0;
192}
193
Al Viro714e85b2006-11-14 20:51:49 -0800194static __inline__ int inet_mask_len(__be32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Al Viro714e85b2006-11-14 20:51:49 -0800196 __u32 hmask = ntohl(mask);
197 if (!hmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
Al Viro714e85b2006-11-14 20:51:49 -0800199 return 32 - ffz(~hmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202
203#endif /* _LINUX_INETDEVICE_H */