inetdevice.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef _LINUX_INETDEVICE_H
  2. #define _LINUX_INETDEVICE_H
  3. #ifdef __KERNEL__
  4. #include <linux/if.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/rcupdate.h>
  7. #include <linux/timer.h>
  8. struct ipv4_devconf
  9. {
  10. int accept_redirects;
  11. int send_redirects;
  12. int secure_redirects;
  13. int shared_media;
  14. int accept_source_route;
  15. int rp_filter;
  16. int proxy_arp;
  17. int bootp_relay;
  18. int log_martians;
  19. int forwarding;
  20. int mc_forwarding;
  21. int tag;
  22. int arp_filter;
  23. int arp_announce;
  24. int arp_ignore;
  25. int arp_accept;
  26. int medium_id;
  27. int no_xfrm;
  28. int no_policy;
  29. int force_igmp_version;
  30. int promote_secondaries;
  31. void *sysctl;
  32. };
  33. extern struct ipv4_devconf ipv4_devconf;
  34. struct in_device
  35. {
  36. struct net_device *dev;
  37. atomic_t refcnt;
  38. int dead;
  39. struct in_ifaddr *ifa_list; /* IP ifaddr chain */
  40. rwlock_t mc_list_lock;
  41. struct ip_mc_list *mc_list; /* IP multicast filter chain */
  42. spinlock_t mc_tomb_lock;
  43. struct ip_mc_list *mc_tomb;
  44. unsigned long mr_v1_seen;
  45. unsigned long mr_v2_seen;
  46. unsigned long mr_maxdelay;
  47. unsigned char mr_qrv;
  48. unsigned char mr_gq_running;
  49. unsigned char mr_ifc_count;
  50. struct timer_list mr_gq_timer; /* general query timer */
  51. struct timer_list mr_ifc_timer; /* interface change timer */
  52. struct neigh_parms *arp_parms;
  53. struct ipv4_devconf cnf;
  54. struct rcu_head rcu_head;
  55. };
  56. #define IN_DEV_FORWARD(in_dev) ((in_dev)->cnf.forwarding)
  57. #define IN_DEV_MFORWARD(in_dev) (ipv4_devconf.mc_forwarding && (in_dev)->cnf.mc_forwarding)
  58. #define IN_DEV_RPFILTER(in_dev) (ipv4_devconf.rp_filter && (in_dev)->cnf.rp_filter)
  59. #define IN_DEV_SOURCE_ROUTE(in_dev) (ipv4_devconf.accept_source_route && (in_dev)->cnf.accept_source_route)
  60. #define IN_DEV_BOOTP_RELAY(in_dev) (ipv4_devconf.bootp_relay && (in_dev)->cnf.bootp_relay)
  61. #define IN_DEV_LOG_MARTIANS(in_dev) (ipv4_devconf.log_martians || (in_dev)->cnf.log_martians)
  62. #define IN_DEV_PROXY_ARP(in_dev) (ipv4_devconf.proxy_arp || (in_dev)->cnf.proxy_arp)
  63. #define IN_DEV_SHARED_MEDIA(in_dev) (ipv4_devconf.shared_media || (in_dev)->cnf.shared_media)
  64. #define IN_DEV_TX_REDIRECTS(in_dev) (ipv4_devconf.send_redirects || (in_dev)->cnf.send_redirects)
  65. #define IN_DEV_SEC_REDIRECTS(in_dev) (ipv4_devconf.secure_redirects || (in_dev)->cnf.secure_redirects)
  66. #define IN_DEV_IDTAG(in_dev) ((in_dev)->cnf.tag)
  67. #define IN_DEV_MEDIUM_ID(in_dev) ((in_dev)->cnf.medium_id)
  68. #define IN_DEV_PROMOTE_SECONDARIES(in_dev) (ipv4_devconf.promote_secondaries || (in_dev)->cnf.promote_secondaries)
  69. #define IN_DEV_RX_REDIRECTS(in_dev) \
  70. ((IN_DEV_FORWARD(in_dev) && \
  71. (ipv4_devconf.accept_redirects && (in_dev)->cnf.accept_redirects)) \
  72. || (!IN_DEV_FORWARD(in_dev) && \
  73. (ipv4_devconf.accept_redirects || (in_dev)->cnf.accept_redirects)))
  74. #define IN_DEV_ARPFILTER(in_dev) (ipv4_devconf.arp_filter || (in_dev)->cnf.arp_filter)
  75. #define IN_DEV_ARP_ANNOUNCE(in_dev) (max(ipv4_devconf.arp_announce, (in_dev)->cnf.arp_announce))
  76. #define IN_DEV_ARP_IGNORE(in_dev) (max(ipv4_devconf.arp_ignore, (in_dev)->cnf.arp_ignore))
  77. struct in_ifaddr
  78. {
  79. struct in_ifaddr *ifa_next;
  80. struct in_device *ifa_dev;
  81. struct rcu_head rcu_head;
  82. u32 ifa_local;
  83. u32 ifa_address;
  84. u32 ifa_mask;
  85. u32 ifa_broadcast;
  86. u32 ifa_anycast;
  87. unsigned char ifa_scope;
  88. unsigned char ifa_flags;
  89. unsigned char ifa_prefixlen;
  90. char ifa_label[IFNAMSIZ];
  91. };
  92. extern int register_inetaddr_notifier(struct notifier_block *nb);
  93. extern int unregister_inetaddr_notifier(struct notifier_block *nb);
  94. extern struct net_device *ip_dev_find(u32 addr);
  95. extern int inet_addr_onlink(struct in_device *in_dev, u32 a, u32 b);
  96. extern int devinet_ioctl(unsigned int cmd, void __user *);
  97. extern void devinet_init(void);
  98. extern struct in_device *inetdev_init(struct net_device *dev);
  99. extern struct in_device *inetdev_by_index(int);
  100. extern u32 inet_select_addr(const struct net_device *dev, u32 dst, int scope);
  101. extern u32 inet_confirm_addr(const struct net_device *dev, u32 dst, u32 local, int scope);
  102. extern struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, u32 prefix, u32 mask);
  103. extern void inet_forward_change(void);
  104. static __inline__ int inet_ifa_match(u32 addr, struct in_ifaddr *ifa)
  105. {
  106. return !((addr^ifa->ifa_address)&ifa->ifa_mask);
  107. }
  108. /*
  109. * Check if a mask is acceptable.
  110. */
  111. static __inline__ int bad_mask(u32 mask, u32 addr)
  112. {
  113. if (addr & (mask = ~mask))
  114. return 1;
  115. mask = ntohl(mask);
  116. if (mask & (mask+1))
  117. return 1;
  118. return 0;
  119. }
  120. #define for_primary_ifa(in_dev) { struct in_ifaddr *ifa; \
  121. for (ifa = (in_dev)->ifa_list; ifa && !(ifa->ifa_flags&IFA_F_SECONDARY); ifa = ifa->ifa_next)
  122. #define for_ifa(in_dev) { struct in_ifaddr *ifa; \
  123. for (ifa = (in_dev)->ifa_list; ifa; ifa = ifa->ifa_next)
  124. #define endfor_ifa(in_dev) }
  125. static inline struct in_device *__in_dev_get_rcu(const struct net_device *dev)
  126. {
  127. struct in_device *in_dev = dev->ip_ptr;
  128. if (in_dev)
  129. in_dev = rcu_dereference(in_dev);
  130. return in_dev;
  131. }
  132. static __inline__ struct in_device *
  133. in_dev_get(const struct net_device *dev)
  134. {
  135. struct in_device *in_dev;
  136. rcu_read_lock();
  137. in_dev = __in_dev_get_rcu(dev);
  138. if (in_dev)
  139. atomic_inc(&in_dev->refcnt);
  140. rcu_read_unlock();
  141. return in_dev;
  142. }
  143. static __inline__ struct in_device *
  144. __in_dev_get_rtnl(const struct net_device *dev)
  145. {
  146. return (struct in_device*)dev->ip_ptr;
  147. }
  148. extern void in_dev_finish_destroy(struct in_device *idev);
  149. static inline void in_dev_put(struct in_device *idev)
  150. {
  151. if (atomic_dec_and_test(&idev->refcnt))
  152. in_dev_finish_destroy(idev);
  153. }
  154. #define __in_dev_put(idev) atomic_dec(&(idev)->refcnt)
  155. #define in_dev_hold(idev) atomic_inc(&(idev)->refcnt)
  156. #endif /* __KERNEL__ */
  157. static __inline__ __u32 inet_make_mask(int logmask)
  158. {
  159. if (logmask)
  160. return htonl(~((1<<(32-logmask))-1));
  161. return 0;
  162. }
  163. static __inline__ int inet_mask_len(__u32 mask)
  164. {
  165. if (!(mask = ntohl(mask)))
  166. return 0;
  167. return 32 - ffz(~mask);
  168. }
  169. #endif /* _LINUX_INETDEVICE_H */