device.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <libubox/kvlist.h>
  19. #include <netinet/in.h>
  20. struct device;
  21. struct device_type;
  22. struct device_user;
  23. struct device_hotplug_ops;
  24. struct bridge_vlan;
  25. struct interface;
  26. typedef int (*device_state_cb)(struct device *, bool up);
  27. enum {
  28. DEV_ATTR_TYPE,
  29. DEV_ATTR_MTU,
  30. DEV_ATTR_MTU6,
  31. DEV_ATTR_MACADDR,
  32. DEV_ATTR_TXQUEUELEN,
  33. DEV_ATTR_ENABLED,
  34. DEV_ATTR_IPV6,
  35. DEV_ATTR_PROMISC,
  36. DEV_ATTR_RPFILTER,
  37. DEV_ATTR_ACCEPTLOCAL,
  38. DEV_ATTR_IGMPVERSION,
  39. DEV_ATTR_MLDVERSION,
  40. DEV_ATTR_NEIGHREACHABLETIME,
  41. DEV_ATTR_DADTRANSMITS,
  42. DEV_ATTR_MULTICAST_TO_UNICAST,
  43. DEV_ATTR_MULTICAST_ROUTER,
  44. DEV_ATTR_MULTICAST_FAST_LEAVE,
  45. DEV_ATTR_MULTICAST,
  46. DEV_ATTR_LEARNING,
  47. DEV_ATTR_UNICAST_FLOOD,
  48. DEV_ATTR_NEIGHGCSTALETIME,
  49. DEV_ATTR_SENDREDIRECTS,
  50. DEV_ATTR_NEIGHLOCKTIME,
  51. DEV_ATTR_ISOLATE,
  52. DEV_ATTR_IP6SEGMENTROUTING,
  53. DEV_ATTR_DROP_V4_UNICAST_IN_L2_MULTICAST,
  54. DEV_ATTR_DROP_V6_UNICAST_IN_L2_MULTICAST,
  55. DEV_ATTR_DROP_GRATUITOUS_ARP,
  56. DEV_ATTR_DROP_UNSOLICITED_NA,
  57. DEV_ATTR_ARP_ACCEPT,
  58. DEV_ATTR_AUTH,
  59. __DEV_ATTR_MAX,
  60. };
  61. enum dev_change_type {
  62. DEV_CONFIG_NO_CHANGE,
  63. DEV_CONFIG_APPLIED,
  64. DEV_CONFIG_RESTART,
  65. DEV_CONFIG_RECREATE,
  66. };
  67. struct device_type {
  68. struct list_head list;
  69. const char *name;
  70. bool bridge_capability;
  71. const char *name_prefix;
  72. const struct uci_blob_param_list *config_params;
  73. struct device *(*create)(const char *name, struct device_type *devtype,
  74. struct blob_attr *attr);
  75. void (*config_init)(struct device *);
  76. enum dev_change_type (*reload)(struct device *, struct blob_attr *);
  77. void (*dump_info)(struct device *, struct blob_buf *buf);
  78. void (*dump_stats)(struct device *, struct blob_buf *buf);
  79. int (*check_state)(struct device *);
  80. void (*free)(struct device *);
  81. };
  82. enum {
  83. DEV_OPT_MTU = (1 << 0),
  84. DEV_OPT_MACADDR = (1 << 1),
  85. DEV_OPT_TXQUEUELEN = (1 << 2),
  86. DEV_OPT_IPV6 = (1 << 3),
  87. DEV_OPT_PROMISC = (1 << 4),
  88. DEV_OPT_RPFILTER = (1 << 5),
  89. DEV_OPT_ACCEPTLOCAL = (1 << 6),
  90. DEV_OPT_IGMPVERSION = (1 << 7),
  91. DEV_OPT_MLDVERSION = (1 << 8),
  92. DEV_OPT_NEIGHREACHABLETIME = (1 << 9),
  93. DEV_OPT_DEFAULT_MACADDR = (1 << 10),
  94. DEV_OPT_AUTH = (1 << 11),
  95. DEV_OPT_MTU6 = (1 << 12),
  96. DEV_OPT_DADTRANSMITS = (1 << 13),
  97. DEV_OPT_MULTICAST_TO_UNICAST = (1 << 14),
  98. DEV_OPT_MULTICAST_ROUTER = (1 << 15),
  99. DEV_OPT_MULTICAST = (1 << 16),
  100. DEV_OPT_LEARNING = (1 << 17),
  101. DEV_OPT_UNICAST_FLOOD = (1 << 18),
  102. DEV_OPT_NEIGHGCSTALETIME = (1 << 19),
  103. DEV_OPT_MULTICAST_FAST_LEAVE = (1 << 20),
  104. DEV_OPT_SENDREDIRECTS = (1 << 21),
  105. DEV_OPT_NEIGHLOCKTIME = (1 << 22),
  106. DEV_OPT_ISOLATE = (1 << 23),
  107. DEV_OPT_IP6SEGMENTROUTING = (1 << 24),
  108. DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST = (1 << 25),
  109. DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST = (1 << 26),
  110. DEV_OPT_DROP_GRATUITOUS_ARP = (1 << 27),
  111. DEV_OPT_DROP_UNSOLICITED_NA = (1 << 28),
  112. DEV_OPT_ARP_ACCEPT = (1 << 29),
  113. };
  114. /* events broadcasted to all users of a device */
  115. enum device_event {
  116. DEV_EVENT_ADD,
  117. DEV_EVENT_REMOVE,
  118. DEV_EVENT_UPDATE_IFNAME,
  119. DEV_EVENT_UPDATE_IFINDEX,
  120. DEV_EVENT_SETUP,
  121. DEV_EVENT_TEARDOWN,
  122. DEV_EVENT_UP,
  123. DEV_EVENT_DOWN,
  124. DEV_EVENT_AUTH_UP,
  125. DEV_EVENT_LINK_UP,
  126. DEV_EVENT_LINK_DOWN,
  127. /* Topology changed (i.e. bridge member added) */
  128. DEV_EVENT_TOPO_CHANGE,
  129. __DEV_EVENT_MAX
  130. };
  131. /*
  132. * device dependency with callbacks
  133. */
  134. struct device_user {
  135. struct safe_list list;
  136. bool claimed;
  137. bool hotplug;
  138. bool alias;
  139. uint8_t ev_idx[__DEV_EVENT_MAX];
  140. struct device *dev;
  141. void (*cb)(struct device_user *, enum device_event);
  142. };
  143. struct device_settings {
  144. unsigned int flags;
  145. unsigned int valid_flags;
  146. unsigned int mtu;
  147. unsigned int mtu6;
  148. unsigned int txqueuelen;
  149. uint8_t macaddr[6];
  150. bool ipv6;
  151. bool promisc;
  152. unsigned int rpfilter;
  153. bool acceptlocal;
  154. unsigned int igmpversion;
  155. unsigned int mldversion;
  156. unsigned int neigh4reachabletime;
  157. unsigned int neigh6reachabletime;
  158. unsigned int neigh4gcstaletime;
  159. unsigned int neigh6gcstaletime;
  160. int neigh4locktime;
  161. unsigned int dadtransmits;
  162. bool multicast_to_unicast;
  163. unsigned int multicast_router;
  164. bool multicast_fast_leave;
  165. bool multicast;
  166. bool learning;
  167. bool unicast_flood;
  168. bool sendredirects;
  169. bool ip6segmentrouting;
  170. bool isolate;
  171. bool drop_v4_unicast_in_l2_multicast;
  172. bool drop_v6_unicast_in_l2_multicast;
  173. bool drop_gratuitous_arp;
  174. bool drop_unsolicited_na;
  175. bool arp_accept;
  176. bool auth;
  177. };
  178. /*
  179. * link layer device. typically represents a linux network device.
  180. * can be used to support VLANs as well
  181. */
  182. struct device {
  183. struct device_type *type;
  184. struct avl_node avl;
  185. struct safe_list users;
  186. struct safe_list aliases;
  187. struct vlist_tree vlans;
  188. struct kvlist vlan_aliases;
  189. char ifname[IFNAMSIZ + 1];
  190. int ifindex;
  191. struct blob_attr *config;
  192. bool config_pending;
  193. bool sys_present;
  194. /* DEV_EVENT_ADD */
  195. bool present;
  196. /* DEV_EVENT_UP */
  197. int active;
  198. /* DEV_EVENT_LINK_UP */
  199. bool link_active;
  200. bool auth_status;
  201. bool external;
  202. bool disabled;
  203. bool deferred;
  204. bool hidden;
  205. bool current_config;
  206. bool iface_config;
  207. bool default_config;
  208. bool wireless;
  209. bool wireless_ap;
  210. bool wireless_isolate;
  211. struct interface *config_iface;
  212. /* set interface up or down */
  213. device_state_cb set_state;
  214. const struct device_hotplug_ops *hotplug_ops;
  215. struct device_user parent;
  216. struct device_settings orig_settings;
  217. struct device_settings settings;
  218. };
  219. struct device_hotplug_ops {
  220. int (*prepare)(struct device *dev, struct device **bridge_dev);
  221. int (*add)(struct device *main, struct device *member, struct blob_attr *vlan);
  222. int (*del)(struct device *main, struct device *member, struct blob_attr *vlan);
  223. };
  224. enum bridge_vlan_flags {
  225. BRVLAN_F_SELF = (1 << 0),
  226. BRVLAN_F_PVID = (1 << 1),
  227. BRVLAN_F_UNTAGGED = (1 << 2),
  228. };
  229. struct bridge_vlan_port {
  230. const char *ifname;
  231. uint16_t flags;
  232. };
  233. struct bridge_vlan {
  234. struct vlist_node node;
  235. struct bridge_vlan_port *ports;
  236. int n_ports;
  237. struct list_head hotplug_ports;
  238. uint16_t vid;
  239. bool local;
  240. };
  241. extern const struct uci_blob_param_list device_attr_list;
  242. extern struct device_type simple_device_type;
  243. extern struct device_type tunnel_device_type;
  244. void device_lock(void);
  245. void device_unlock(void);
  246. void device_vlan_update(bool done);
  247. int device_type_add(struct device_type *devtype);
  248. struct device_type *device_type_get(const char *tname);
  249. struct device *device_create(const char *name, struct device_type *type,
  250. struct blob_attr *config);
  251. void device_merge_settings(struct device *dev, struct device_settings *n);
  252. void device_init_settings(struct device *dev, struct blob_attr **tb);
  253. void device_init_pending(void);
  254. enum dev_change_type
  255. device_apply_config(struct device *dev, struct device_type *type,
  256. struct blob_attr *config);
  257. void device_reset_config(void);
  258. void device_reset_old(void);
  259. int device_init_virtual(struct device *dev, struct device_type *type, const char *name);
  260. int device_init(struct device *dev, struct device_type *type, const char *ifname);
  261. void device_cleanup(struct device *dev);
  262. struct device *device_find(const char *name);
  263. struct device *device_get(const char *name, int create);
  264. void device_add_user(struct device_user *dep, struct device *dev);
  265. void device_remove_user(struct device_user *dep);
  266. void device_broadcast_event(struct device *dev, enum device_event ev);
  267. void device_set_present(struct device *dev, bool state);
  268. void device_set_link(struct device *dev, bool state);
  269. void device_set_ifindex(struct device *dev, int ifindex);
  270. int device_set_ifname(struct device *dev, const char *name);
  271. void device_refresh_present(struct device *dev);
  272. int device_claim(struct device_user *dep);
  273. void device_release(struct device_user *dep);
  274. int device_check_state(struct device *dev);
  275. void device_dump_status(struct blob_buf *b, struct device *dev);
  276. void device_free_unused(struct device *dev);
  277. struct device *get_vlan_device_chain(const char *ifname, bool create);
  278. void alias_notify_device(const char *name, struct device *dev);
  279. struct device *device_alias_get(const char *name);
  280. void device_set_auth_status(struct device *dev, bool value);
  281. static inline void
  282. device_set_deferred(struct device *dev, bool value)
  283. {
  284. dev->deferred = value;
  285. device_refresh_present(dev);
  286. }
  287. static inline void
  288. device_set_disabled(struct device *dev, bool value)
  289. {
  290. dev->disabled = value;
  291. device_refresh_present(dev);
  292. }
  293. static inline bool
  294. device_link_active(struct device *dev)
  295. {
  296. if (dev->settings.auth && !dev->auth_status)
  297. return false;
  298. return dev->link_active;
  299. }
  300. bool device_check_ip6segmentrouting(void);
  301. #endif