inetdevice.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef _LINUX_INETDEVICE_H
  2. #define _LINUX_INETDEVICE_H
  3. #ifdef __KERNEL__
  4. #include <linux/bitmap.h>
  5. #include <linux/if.h>
  6. #include <linux/ip.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/rcupdate.h>
  9. #include <linux/timer.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/rtnetlink.h>
  12. struct ipv4_devconf {
  13. void *sysctl;
  14. int data[IPV4_DEVCONF_MAX];
  15. DECLARE_BITMAP(state, IPV4_DEVCONF_MAX);
  16. };
  17. #define MC_HASH_SZ_LOG 9
  18. struct in_device {
  19. struct net_device *dev;
  20. atomic_t refcnt;
  21. int dead;
  22. struct in_ifaddr *ifa_list; /* IP ifaddr chain */
  23. struct ip_mc_list __rcu *mc_list; /* IP multicast filter chain */
  24. struct ip_mc_list __rcu * __rcu *mc_hash;
  25. int mc_count; /* Number of installed mcasts */
  26. spinlock_t mc_tomb_lock;
  27. struct ip_mc_list *mc_tomb;
  28. unsigned long mr_v1_seen;
  29. unsigned long mr_v2_seen;
  30. unsigned long mr_maxdelay;
  31. unsigned char mr_qrv;
  32. unsigned char mr_gq_running;
  33. unsigned char mr_ifc_count;
  34. struct timer_list mr_gq_timer; /* general query timer */
  35. struct timer_list mr_ifc_timer; /* interface change timer */
  36. struct neigh_parms *arp_parms;
  37. struct ipv4_devconf cnf;
  38. struct rcu_head rcu_head;
  39. };
  40. #define IPV4_DEVCONF(cnf, attr) ((cnf).data[IPV4_DEVCONF_ ## attr - 1])
  41. #define IPV4_DEVCONF_ALL(net, attr) \
  42. IPV4_DEVCONF((*(net)->ipv4.devconf_all), attr)
  43. static inline int ipv4_devconf_get(struct in_device *in_dev, int index)
  44. {
  45. index--;
  46. return in_dev->cnf.data[index];
  47. }
  48. static inline void ipv4_devconf_set(struct in_device *in_dev, int index,
  49. int val)
  50. {
  51. index--;
  52. set_bit(index, in_dev->cnf.state);
  53. in_dev->cnf.data[index] = val;
  54. }
  55. static inline void ipv4_devconf_setall(struct in_device *in_dev)
  56. {
  57. bitmap_fill(in_dev->cnf.state, IPV4_DEVCONF_MAX);
  58. }
  59. #define IN_DEV_CONF_GET(in_dev, attr) \
  60. ipv4_devconf_get((in_dev), IPV4_DEVCONF_ ## attr)
  61. #define IN_DEV_CONF_SET(in_dev, attr, val) \
  62. ipv4_devconf_set((in_dev), IPV4_DEVCONF_ ## attr, (val))
  63. #define IN_DEV_ANDCONF(in_dev, attr) \
  64. (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), attr) && \
  65. IN_DEV_CONF_GET((in_dev), attr))
  66. #define IN_DEV_NET_ORCONF(in_dev, net, attr) \
  67. (IPV4_DEVCONF_ALL(net, attr) || \
  68. IN_DEV_CONF_GET((in_dev), attr))
  69. #define IN_DEV_ORCONF(in_dev, attr) \
  70. IN_DEV_NET_ORCONF(in_dev, dev_net(in_dev->dev), attr)
  71. #define IN_DEV_MAXCONF(in_dev, attr) \
  72. (max(IPV4_DEVCONF_ALL(dev_net(in_dev->dev), attr), \
  73. IN_DEV_CONF_GET((in_dev), attr)))
  74. #define IN_DEV_FORWARD(in_dev) IN_DEV_CONF_GET((in_dev), FORWARDING)
  75. #define IN_DEV_MFORWARD(in_dev) IN_DEV_ANDCONF((in_dev), MC_FORWARDING)
  76. #define IN_DEV_RPFILTER(in_dev) IN_DEV_MAXCONF((in_dev), RP_FILTER)
  77. #define IN_DEV_SRC_VMARK(in_dev) IN_DEV_ORCONF((in_dev), SRC_VMARK)
  78. #define IN_DEV_SOURCE_ROUTE(in_dev) IN_DEV_ANDCONF((in_dev), \
  79. ACCEPT_SOURCE_ROUTE)
  80. #define IN_DEV_ACCEPT_LOCAL(in_dev) IN_DEV_ORCONF((in_dev), ACCEPT_LOCAL)
  81. #define IN_DEV_BOOTP_RELAY(in_dev) IN_DEV_ANDCONF((in_dev), BOOTP_RELAY)
  82. #define IN_DEV_LOG_MARTIANS(in_dev) IN_DEV_ORCONF((in_dev), LOG_MARTIANS)
  83. #define IN_DEV_PROXY_ARP(in_dev) IN_DEV_ORCONF((in_dev), PROXY_ARP)
  84. #define IN_DEV_PROXY_ARP_PVLAN(in_dev) IN_DEV_CONF_GET(in_dev, PROXY_ARP_PVLAN)
  85. #define IN_DEV_SHARED_MEDIA(in_dev) IN_DEV_ORCONF((in_dev), SHARED_MEDIA)
  86. #define IN_DEV_TX_REDIRECTS(in_dev) IN_DEV_ORCONF((in_dev), SEND_REDIRECTS)
  87. #define IN_DEV_SEC_REDIRECTS(in_dev) IN_DEV_ORCONF((in_dev), \
  88. SECURE_REDIRECTS)
  89. #define IN_DEV_IDTAG(in_dev) IN_DEV_CONF_GET(in_dev, TAG)
  90. #define IN_DEV_MEDIUM_ID(in_dev) IN_DEV_CONF_GET(in_dev, MEDIUM_ID)
  91. #define IN_DEV_PROMOTE_SECONDARIES(in_dev) \
  92. IN_DEV_ORCONF((in_dev), \
  93. PROMOTE_SECONDARIES)
  94. #define IN_DEV_ROUTE_LOCALNET(in_dev) IN_DEV_ORCONF(in_dev, ROUTE_LOCALNET)
  95. #define IN_DEV_NET_ROUTE_LOCALNET(in_dev, net) \
  96. IN_DEV_NET_ORCONF(in_dev, net, ROUTE_LOCALNET)
  97. #define IN_DEV_RX_REDIRECTS(in_dev) \
  98. ((IN_DEV_FORWARD(in_dev) && \
  99. IN_DEV_ANDCONF((in_dev), ACCEPT_REDIRECTS)) \
  100. || (!IN_DEV_FORWARD(in_dev) && \
  101. IN_DEV_ORCONF((in_dev), ACCEPT_REDIRECTS)))
  102. #define IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) \
  103. IN_DEV_CONF_GET((in_dev), IGNORE_ROUTES_WITH_LINKDOWN)
  104. #define IN_DEV_ARPFILTER(in_dev) IN_DEV_ORCONF((in_dev), ARPFILTER)
  105. #define IN_DEV_ARP_ACCEPT(in_dev) IN_DEV_ORCONF((in_dev), ARP_ACCEPT)
  106. #define IN_DEV_ARP_ANNOUNCE(in_dev) IN_DEV_MAXCONF((in_dev), ARP_ANNOUNCE)
  107. #define IN_DEV_ARP_IGNORE(in_dev) IN_DEV_MAXCONF((in_dev), ARP_IGNORE)
  108. #define IN_DEV_ARP_NOTIFY(in_dev) IN_DEV_MAXCONF((in_dev), ARP_NOTIFY)
  109. struct in_ifaddr {
  110. struct hlist_node hash;
  111. struct in_ifaddr *ifa_next;
  112. struct in_device *ifa_dev;
  113. struct rcu_head rcu_head;
  114. __be32 ifa_local;
  115. __be32 ifa_address;
  116. __be32 ifa_mask;
  117. __be32 ifa_broadcast;
  118. unsigned char ifa_scope;
  119. unsigned char ifa_prefixlen;
  120. __u32 ifa_flags;
  121. char ifa_label[IFNAMSIZ];
  122. /* In seconds, relative to tstamp. Expiry is at tstamp + HZ * lft. */
  123. __u32 ifa_valid_lft;
  124. __u32 ifa_preferred_lft;
  125. unsigned long ifa_cstamp; /* created timestamp */
  126. unsigned long ifa_tstamp; /* updated timestamp */
  127. };
  128. int register_inetaddr_notifier(struct notifier_block *nb);
  129. int unregister_inetaddr_notifier(struct notifier_block *nb);
  130. void inet_netconf_notify_devconf(struct net *net, int type, int ifindex,
  131. struct ipv4_devconf *devconf);
  132. struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref);
  133. static inline struct net_device *ip_dev_find(struct net *net, __be32 addr)
  134. {
  135. return __ip_dev_find(net, addr, true);
  136. }
  137. int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b);
  138. int devinet_ioctl(struct net *net, unsigned int cmd, void __user *);
  139. void devinet_init(void);
  140. struct in_device *inetdev_by_index(struct net *, int);
  141. __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope);
  142. __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, __be32 dst,
  143. __be32 local, int scope);
  144. struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
  145. __be32 mask);
  146. static __inline__ bool inet_ifa_match(__be32 addr, struct in_ifaddr *ifa)
  147. {
  148. return !((addr^ifa->ifa_address)&ifa->ifa_mask);
  149. }
  150. /*
  151. * Check if a mask is acceptable.
  152. */
  153. static __inline__ bool bad_mask(__be32 mask, __be32 addr)
  154. {
  155. __u32 hmask;
  156. if (addr & (mask = ~mask))
  157. return true;
  158. hmask = ntohl(mask);
  159. if (hmask & (hmask+1))
  160. return true;
  161. return false;
  162. }
  163. #define for_primary_ifa(in_dev) { struct in_ifaddr *ifa; \
  164. for (ifa = (in_dev)->ifa_list; ifa && !(ifa->ifa_flags&IFA_F_SECONDARY); ifa = ifa->ifa_next)
  165. #define for_ifa(in_dev) { struct in_ifaddr *ifa; \
  166. for (ifa = (in_dev)->ifa_list; ifa; ifa = ifa->ifa_next)
  167. #define endfor_ifa(in_dev) }
  168. static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
  169. {
  170. return rcu_dereference(dev->ip_ptr);
  171. }
  172. static inline struct in_device *in_dev_get(const struct net_device *dev)
  173. {
  174. struct in_device *in_dev;
  175. rcu_read_lock();
  176. in_dev = __in_dev_get_rcu(dev);
  177. if (in_dev)
  178. atomic_inc(&in_dev->refcnt);
  179. rcu_read_unlock();
  180. return in_dev;
  181. }
  182. static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
  183. {
  184. return rtnl_dereference(dev->ip_ptr);
  185. }
  186. static inline struct neigh_parms *__in_dev_arp_parms_get_rcu(const struct net_device *dev)
  187. {
  188. struct in_device *in_dev = __in_dev_get_rcu(dev);
  189. return in_dev ? in_dev->arp_parms : NULL;
  190. }
  191. void in_dev_finish_destroy(struct in_device *idev);
  192. static inline void in_dev_put(struct in_device *idev)
  193. {
  194. if (atomic_dec_and_test(&idev->refcnt))
  195. in_dev_finish_destroy(idev);
  196. }
  197. #define __in_dev_put(idev) atomic_dec(&(idev)->refcnt)
  198. #define in_dev_hold(idev) atomic_inc(&(idev)->refcnt)
  199. #endif /* __KERNEL__ */
  200. static __inline__ __be32 inet_make_mask(int logmask)
  201. {
  202. if (logmask)
  203. return htonl(~((1U<<(32-logmask))-1));
  204. return 0;
  205. }
  206. static __inline__ int inet_mask_len(__be32 mask)
  207. {
  208. __u32 hmask = ntohl(mask);
  209. if (!hmask)
  210. return 0;
  211. return 32 - ffz(~hmask);
  212. }
  213. #endif /* _LINUX_INETDEVICE_H */