inetdevice.h 8.7 KB

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