]> bbs.cooldavid.org Git - net-next-2.6.git/blob - include/linux/inetdevice.h
[IPV4]: Convert IPv4 devconf to an array
[net-next-2.6.git] / include / linux / inetdevice.h
1 #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
11 struct ipv4_devconf
12 {
13         void    *sysctl;
14         int     data[__NET_IPV4_CONF_MAX - 1];
15 };
16
17 extern struct ipv4_devconf ipv4_devconf;
18
19 struct in_device
20 {
21         struct net_device       *dev;
22         atomic_t                refcnt;
23         int                     dead;
24         struct in_ifaddr        *ifa_list;      /* IP ifaddr chain              */
25         rwlock_t                mc_list_lock;
26         struct ip_mc_list       *mc_list;       /* IP multicast filter chain    */
27         spinlock_t              mc_tomb_lock;
28         struct ip_mc_list       *mc_tomb;
29         unsigned long           mr_v1_seen;
30         unsigned long           mr_v2_seen;
31         unsigned long           mr_maxdelay;
32         unsigned char           mr_qrv;
33         unsigned char           mr_gq_running;
34         unsigned char           mr_ifc_count;
35         struct timer_list       mr_gq_timer;    /* general query timer */
36         struct timer_list       mr_ifc_timer;   /* interface change timer */
37
38         struct neigh_parms      *arp_parms;
39         struct ipv4_devconf     cnf;
40         struct rcu_head         rcu_head;
41 };
42
43 #define IPV4_DEVCONF(cnf, attr) ((cnf).data[NET_IPV4_CONF_ ## attr - 1])
44 #define IPV4_DEVCONF_ALL(attr) IPV4_DEVCONF(ipv4_devconf, attr)
45
46 static inline int ipv4_devconf_get(struct in_device *in_dev, int index)
47 {
48         index--;
49         return in_dev->cnf.data[index];
50 }
51
52 static inline void ipv4_devconf_set(struct in_device *in_dev, int index,
53                                     int val)
54 {
55         index--;
56         in_dev->cnf.data[index] = val;
57 }
58
59 #define IN_DEV_CONF_GET(in_dev, attr) \
60         ipv4_devconf_get((in_dev), NET_IPV4_CONF_ ## attr)
61 #define IN_DEV_CONF_SET(in_dev, attr, val) \
62         ipv4_devconf_set((in_dev), NET_IPV4_CONF_ ## attr, (val))
63
64 #define IN_DEV_ANDCONF(in_dev, attr) \
65         (IPV4_DEVCONF_ALL(attr) && IN_DEV_CONF_GET((in_dev), attr))
66 #define IN_DEV_ORCONF(in_dev, attr) \
67         (IPV4_DEVCONF_ALL(attr) || IN_DEV_CONF_GET((in_dev), attr))
68 #define IN_DEV_MAXCONF(in_dev, attr) \
69         (max(IPV4_DEVCONF_ALL(attr), IN_DEV_CONF_GET((in_dev), attr)))
70
71 #define IN_DEV_FORWARD(in_dev)          IN_DEV_CONF_GET((in_dev), FORWARDING)
72 #define IN_DEV_MFORWARD(in_dev)         (IPV4_DEVCONF_ALL(MC_FORWARDING) && \
73                                          IPV4_DEVCONF((in_dev)->cnf, \
74                                                       MC_FORWARDING))
75 #define IN_DEV_RPFILTER(in_dev)         IN_DEV_ANDCONF((in_dev), RP_FILTER)
76 #define IN_DEV_SOURCE_ROUTE(in_dev)     IN_DEV_ANDCONF((in_dev), \
77                                                        ACCEPT_SOURCE_ROUTE)
78 #define IN_DEV_BOOTP_RELAY(in_dev)      IN_DEV_ANDCONF((in_dev), BOOTP_RELAY)
79
80 #define IN_DEV_LOG_MARTIANS(in_dev)     IN_DEV_ORCONF((in_dev), LOG_MARTIANS)
81 #define IN_DEV_PROXY_ARP(in_dev)        IN_DEV_ORCONF((in_dev), PROXY_ARP)
82 #define IN_DEV_SHARED_MEDIA(in_dev)     IN_DEV_ORCONF((in_dev), SHARED_MEDIA)
83 #define IN_DEV_TX_REDIRECTS(in_dev)     IN_DEV_ORCONF((in_dev), SEND_REDIRECTS)
84 #define IN_DEV_SEC_REDIRECTS(in_dev)    IN_DEV_ORCONF((in_dev), \
85                                                       SECURE_REDIRECTS)
86 #define IN_DEV_IDTAG(in_dev)            IN_DEV_CONF_GET(in_dev, TAG)
87 #define IN_DEV_MEDIUM_ID(in_dev)        IN_DEV_CONF_GET(in_dev, MEDIUM_ID)
88 #define IN_DEV_PROMOTE_SECONDARIES(in_dev) \
89                                         IN_DEV_ORCONF((in_dev), \
90                                                       PROMOTE_SECONDARIES)
91
92 #define IN_DEV_RX_REDIRECTS(in_dev) \
93         ((IN_DEV_FORWARD(in_dev) && \
94           IN_DEV_ANDCONF((in_dev), ACCEPT_REDIRECTS)) \
95          || (!IN_DEV_FORWARD(in_dev) && \
96           IN_DEV_ORCONF((in_dev), ACCEPT_REDIRECTS)))
97
98 #define IN_DEV_ARPFILTER(in_dev)        IN_DEV_ORCONF((in_dev), ARPFILTER)
99 #define IN_DEV_ARP_ANNOUNCE(in_dev)     IN_DEV_MAXCONF((in_dev), ARP_ANNOUNCE)
100 #define IN_DEV_ARP_IGNORE(in_dev)       IN_DEV_MAXCONF((in_dev), ARP_IGNORE)
101
102 struct in_ifaddr
103 {
104         struct in_ifaddr        *ifa_next;
105         struct in_device        *ifa_dev;
106         struct rcu_head         rcu_head;
107         __be32                  ifa_local;
108         __be32                  ifa_address;
109         __be32                  ifa_mask;
110         __be32                  ifa_broadcast;
111         __be32                  ifa_anycast;
112         unsigned char           ifa_scope;
113         unsigned char           ifa_flags;
114         unsigned char           ifa_prefixlen;
115         char                    ifa_label[IFNAMSIZ];
116 };
117
118 extern int register_inetaddr_notifier(struct notifier_block *nb);
119 extern int unregister_inetaddr_notifier(struct notifier_block *nb);
120
121 extern struct net_device        *ip_dev_find(__be32 addr);
122 extern int              inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b);
123 extern int              devinet_ioctl(unsigned int cmd, void __user *);
124 extern void             devinet_init(void);
125 extern struct in_device *inetdev_init(struct net_device *dev);
126 extern struct in_device *inetdev_by_index(int);
127 extern __be32           inet_select_addr(const struct net_device *dev, __be32 dst, int scope);
128 extern __be32           inet_confirm_addr(const struct net_device *dev, __be32 dst, __be32 local, int scope);
129 extern struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, __be32 mask);
130 extern void             inet_forward_change(void);
131
132 static __inline__ int inet_ifa_match(__be32 addr, struct in_ifaddr *ifa)
133 {
134         return !((addr^ifa->ifa_address)&ifa->ifa_mask);
135 }
136
137 /*
138  *      Check if a mask is acceptable.
139  */
140  
141 static __inline__ int bad_mask(__be32 mask, __be32 addr)
142 {
143         __u32 hmask;
144         if (addr & (mask = ~mask))
145                 return 1;
146         hmask = ntohl(mask);
147         if (hmask & (hmask+1))
148                 return 1;
149         return 0;
150 }
151
152 #define for_primary_ifa(in_dev) { struct in_ifaddr *ifa; \
153   for (ifa = (in_dev)->ifa_list; ifa && !(ifa->ifa_flags&IFA_F_SECONDARY); ifa = ifa->ifa_next)
154
155 #define for_ifa(in_dev) { struct in_ifaddr *ifa; \
156   for (ifa = (in_dev)->ifa_list; ifa; ifa = ifa->ifa_next)
157
158
159 #define endfor_ifa(in_dev) }
160
161 static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
162 {
163         struct in_device *in_dev = dev->ip_ptr;
164         if (in_dev)
165                 in_dev = rcu_dereference(in_dev);
166         return in_dev;
167 }
168
169 static __inline__ struct in_device *
170 in_dev_get(const struct net_device *dev)
171 {
172         struct in_device *in_dev;
173
174         rcu_read_lock();
175         in_dev = __in_dev_get_rcu(dev);
176         if (in_dev)
177                 atomic_inc(&in_dev->refcnt);
178         rcu_read_unlock();
179         return in_dev;
180 }
181
182 static __inline__ struct in_device *
183 __in_dev_get_rtnl(const struct net_device *dev)
184 {
185         return (struct in_device*)dev->ip_ptr;
186 }
187
188 extern void in_dev_finish_destroy(struct in_device *idev);
189
190 static inline void in_dev_put(struct in_device *idev)
191 {
192         if (atomic_dec_and_test(&idev->refcnt))
193                 in_dev_finish_destroy(idev);
194 }
195
196 #define __in_dev_put(idev)  atomic_dec(&(idev)->refcnt)
197 #define in_dev_hold(idev)   atomic_inc(&(idev)->refcnt)
198
199 #endif /* __KERNEL__ */
200
201 static __inline__ __be32 inet_make_mask(int logmask)
202 {
203         if (logmask)
204                 return htonl(~((1<<(32-logmask))-1));
205         return 0;
206 }
207
208 static __inline__ int inet_mask_len(__be32 mask)
209 {
210         __u32 hmask = ntohl(mask);
211         if (!hmask)
212                 return 0;
213         return 32 - ffz(~hmask);
214 }
215
216
217 #endif /* _LINUX_INETDEVICE_H */