inetdevice.h 9.0 KB

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