device.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __NETIFD_DEVICE_H
  15. #define __NETIFD_DEVICE_H
  16. #include <libubox/avl.h>
  17. #include <libubox/safe_list.h>
  18. #include <netinet/in.h>
  19. struct device;
  20. struct device_type;
  21. struct device_user;
  22. struct device_hotplug_ops;
  23. struct interface;
  24. typedef int (*device_state_cb)(struct device *, bool up);
  25. enum {
  26. DEV_ATTR_TYPE,
  27. DEV_ATTR_MTU,
  28. DEV_ATTR_MTU6,
  29. DEV_ATTR_MACADDR,
  30. DEV_ATTR_TXQUEUELEN,
  31. DEV_ATTR_ENABLED,
  32. DEV_ATTR_IPV6,
  33. DEV_ATTR_PROMISC,
  34. DEV_ATTR_RPFILTER,
  35. DEV_ATTR_ACCEPTLOCAL,
  36. DEV_ATTR_IGMPVERSION,
  37. DEV_ATTR_MLDVERSION,
  38. DEV_ATTR_NEIGHREACHABLETIME,
  39. DEV_ATTR_DADTRANSMITS,
  40. DEV_ATTR_MULTICAST_TO_UNICAST,
  41. DEV_ATTR_MULTICAST_ROUTER,
  42. DEV_ATTR_MULTICAST_FAST_LEAVE,
  43. DEV_ATTR_MULTICAST,
  44. DEV_ATTR_LEARNING,
  45. DEV_ATTR_UNICAST_FLOOD,
  46. DEV_ATTR_NEIGHGCSTALETIME,
  47. DEV_ATTR_SENDREDIRECTS,
  48. DEV_ATTR_NEIGHLOCKTIME,
  49. DEV_ATTR_ISOLATE,
  50. __DEV_ATTR_MAX,
  51. };
  52. enum dev_change_type {
  53. DEV_CONFIG_NO_CHANGE,
  54. DEV_CONFIG_APPLIED,
  55. DEV_CONFIG_RESTART,
  56. DEV_CONFIG_RECREATE,
  57. };
  58. struct device_type {
  59. struct list_head list;
  60. const char *name;
  61. bool bridge_capability;
  62. const char *name_prefix;
  63. const struct uci_blob_param_list *config_params;
  64. struct device *(*create)(const char *name, struct device_type *devtype,
  65. struct blob_attr *attr);
  66. void (*config_init)(struct device *);
  67. enum dev_change_type (*reload)(struct device *, struct blob_attr *);
  68. void (*dump_info)(struct device *, struct blob_buf *buf);
  69. void (*dump_stats)(struct device *, struct blob_buf *buf);
  70. int (*check_state)(struct device *);
  71. void (*free)(struct device *);
  72. };
  73. enum {
  74. DEV_OPT_MTU = (1 << 0),
  75. DEV_OPT_MACADDR = (1 << 1),
  76. DEV_OPT_TXQUEUELEN = (1 << 2),
  77. DEV_OPT_IPV6 = (1 << 3),
  78. DEV_OPT_PROMISC = (1 << 4),
  79. DEV_OPT_RPFILTER = (1 << 5),
  80. DEV_OPT_ACCEPTLOCAL = (1 << 6),
  81. DEV_OPT_IGMPVERSION = (1 << 7),
  82. DEV_OPT_MLDVERSION = (1 << 8),
  83. DEV_OPT_NEIGHREACHABLETIME = (1 << 9),
  84. /* 2 bit hole */
  85. DEV_OPT_MTU6 = (1 << 12),
  86. DEV_OPT_DADTRANSMITS = (1 << 13),
  87. DEV_OPT_MULTICAST_TO_UNICAST = (1 << 14),
  88. DEV_OPT_MULTICAST_ROUTER = (1 << 15),
  89. DEV_OPT_MULTICAST = (1 << 16),
  90. DEV_OPT_LEARNING = (1 << 17),
  91. DEV_OPT_UNICAST_FLOOD = (1 << 18),
  92. DEV_OPT_NEIGHGCSTALETIME = (1 << 19),
  93. DEV_OPT_MULTICAST_FAST_LEAVE = (1 << 20),
  94. DEV_OPT_SENDREDIRECTS = (1 << 21),
  95. DEV_OPT_NEIGHLOCKTIME = (1 << 22),
  96. DEV_OPT_ISOLATE = (1 << 23),
  97. };
  98. /* events broadcasted to all users of a device */
  99. enum device_event {
  100. DEV_EVENT_ADD,
  101. DEV_EVENT_REMOVE,
  102. DEV_EVENT_UPDATE_IFNAME,
  103. DEV_EVENT_UPDATE_IFINDEX,
  104. DEV_EVENT_SETUP,
  105. DEV_EVENT_TEARDOWN,
  106. DEV_EVENT_UP,
  107. DEV_EVENT_DOWN,
  108. DEV_EVENT_LINK_UP,
  109. DEV_EVENT_LINK_DOWN,
  110. /* Topology changed (i.e. bridge member added) */
  111. DEV_EVENT_TOPO_CHANGE,
  112. __DEV_EVENT_MAX
  113. };
  114. /*
  115. * device dependency with callbacks
  116. */
  117. struct device_user {
  118. struct safe_list list;
  119. bool claimed;
  120. bool hotplug;
  121. bool alias;
  122. uint8_t ev_idx[__DEV_EVENT_MAX];
  123. struct device *dev;
  124. void (*cb)(struct device_user *, enum device_event);
  125. };
  126. struct device_settings {
  127. unsigned int flags;
  128. unsigned int valid_flags;
  129. unsigned int mtu;
  130. unsigned int mtu6;
  131. unsigned int txqueuelen;
  132. uint8_t macaddr[6];
  133. bool ipv6;
  134. bool promisc;
  135. unsigned int rpfilter;
  136. bool acceptlocal;
  137. unsigned int igmpversion;
  138. unsigned int mldversion;
  139. unsigned int neigh4reachabletime;
  140. unsigned int neigh6reachabletime;
  141. unsigned int neigh4gcstaletime;
  142. unsigned int neigh6gcstaletime;
  143. int neigh4locktime;
  144. unsigned int dadtransmits;
  145. bool multicast_to_unicast;
  146. unsigned int multicast_router;
  147. bool multicast_fast_leave;
  148. bool multicast;
  149. bool learning;
  150. bool unicast_flood;
  151. bool sendredirects;
  152. bool isolate;
  153. };
  154. /*
  155. * link layer device. typically represents a linux network device.
  156. * can be used to support VLANs as well
  157. */
  158. struct device {
  159. struct device_type *type;
  160. struct avl_node avl;
  161. struct safe_list users;
  162. struct safe_list aliases;
  163. char ifname[IFNAMSIZ + 1];
  164. int ifindex;
  165. struct blob_attr *config;
  166. bool config_pending;
  167. bool sys_present;
  168. /* DEV_EVENT_ADD */
  169. bool present;
  170. /* DEV_EVENT_UP */
  171. int active;
  172. /* DEV_EVENT_LINK_UP */
  173. bool link_active;
  174. bool external;
  175. bool disabled;
  176. bool deferred;
  177. bool hidden;
  178. bool current_config;
  179. bool iface_config;
  180. bool default_config;
  181. bool wireless;
  182. bool wireless_ap;
  183. bool wireless_isolate;
  184. struct interface *config_iface;
  185. /* set interface up or down */
  186. device_state_cb set_state;
  187. const struct device_hotplug_ops *hotplug_ops;
  188. struct device_user parent;
  189. struct device_settings orig_settings;
  190. struct device_settings settings;
  191. };
  192. struct device_hotplug_ops {
  193. int (*prepare)(struct device *dev);
  194. int (*add)(struct device *main, struct device *member);
  195. int (*del)(struct device *main, struct device *member);
  196. };
  197. extern const struct uci_blob_param_list device_attr_list;
  198. extern struct device_type simple_device_type;
  199. extern struct device_type tunnel_device_type;
  200. void device_lock(void);
  201. void device_unlock(void);
  202. int device_type_add(struct device_type *devtype);
  203. struct device_type *device_type_get(const char *tname);
  204. struct device *device_create(const char *name, struct device_type *type,
  205. struct blob_attr *config);
  206. void device_merge_settings(struct device *dev, struct device_settings *n);
  207. void device_init_settings(struct device *dev, struct blob_attr **tb);
  208. void device_init_pending(void);
  209. enum dev_change_type
  210. device_apply_config(struct device *dev, struct device_type *type,
  211. struct blob_attr *config);
  212. void device_reset_config(void);
  213. void device_reset_old(void);
  214. int device_init_virtual(struct device *dev, struct device_type *type, const char *name);
  215. int device_init(struct device *dev, struct device_type *type, const char *ifname);
  216. void device_cleanup(struct device *dev);
  217. struct device *device_find(const char *name);
  218. struct device *device_get(const char *name, int create);
  219. void device_add_user(struct device_user *dep, struct device *dev);
  220. void device_remove_user(struct device_user *dep);
  221. void device_broadcast_event(struct device *dev, enum device_event ev);
  222. void device_set_present(struct device *dev, bool state);
  223. void device_set_link(struct device *dev, bool state);
  224. void device_set_ifindex(struct device *dev, int ifindex);
  225. int device_set_ifname(struct device *dev, const char *name);
  226. void device_refresh_present(struct device *dev);
  227. int device_claim(struct device_user *dep);
  228. void device_release(struct device_user *dep);
  229. int device_check_state(struct device *dev);
  230. void device_dump_status(struct blob_buf *b, struct device *dev);
  231. void device_free_unused(struct device *dev);
  232. struct device *get_vlan_device_chain(const char *ifname, bool create);
  233. void alias_notify_device(const char *name, struct device *dev);
  234. struct device *device_alias_get(const char *name);
  235. static inline void
  236. device_set_deferred(struct device *dev, bool value)
  237. {
  238. dev->deferred = value;
  239. device_refresh_present(dev);
  240. }
  241. static inline void
  242. device_set_disabled(struct device *dev, bool value)
  243. {
  244. dev->disabled = value;
  245. device_refresh_present(dev);
  246. }
  247. #endif