if.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Interface related header.
  2. Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #ifndef _ZEBRA_IF_H
  17. #define _ZEBRA_IF_H
  18. #include "linklist.h"
  19. /*
  20. Interface name length.
  21. Linux define value in /usr/include/linux/if.h.
  22. #define IFNAMSIZ 16
  23. FreeBSD define value in /usr/include/net/if.h.
  24. #define IFNAMSIZ 16
  25. */
  26. #define INTERFACE_NAMSIZ 20
  27. #define INTERFACE_HWADDR_MAX 20
  28. #ifdef HAVE_PROC_NET_DEV
  29. struct if_stats
  30. {
  31. unsigned long rx_packets; /* total packets received */
  32. unsigned long tx_packets; /* total packets transmitted */
  33. unsigned long rx_bytes; /* total bytes received */
  34. unsigned long tx_bytes; /* total bytes transmitted */
  35. unsigned long rx_errors; /* bad packets received */
  36. unsigned long tx_errors; /* packet transmit problems */
  37. unsigned long rx_dropped; /* no space in linux buffers */
  38. unsigned long tx_dropped; /* no space available in linux */
  39. unsigned long rx_multicast; /* multicast packets received */
  40. unsigned long rx_compressed;
  41. unsigned long tx_compressed;
  42. unsigned long collisions;
  43. /* detailed rx_errors: */
  44. unsigned long rx_length_errors;
  45. unsigned long rx_over_errors; /* receiver ring buff overflow */
  46. unsigned long rx_crc_errors; /* recved pkt with crc error */
  47. unsigned long rx_frame_errors; /* recv'd frame alignment error */
  48. unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  49. unsigned long rx_missed_errors; /* receiver missed packet */
  50. /* detailed tx_errors */
  51. unsigned long tx_aborted_errors;
  52. unsigned long tx_carrier_errors;
  53. unsigned long tx_fifo_errors;
  54. unsigned long tx_heartbeat_errors;
  55. unsigned long tx_window_errors;
  56. };
  57. #endif /* HAVE_PROC_NET_DEV */
  58. /* Interface structure */
  59. struct interface
  60. {
  61. /* Interface name. This should probably never be changed after the
  62. interface is created, because the configuration info for this interface
  63. is associated with this structure. For that reason, the interface
  64. should also never be deleted (to avoid losing configuration info).
  65. To delete, just set ifindex to IFINDEX_INTERNAL to indicate that the
  66. interface does not exist in the kernel.
  67. */
  68. char name[INTERFACE_NAMSIZ + 1];
  69. /* Interface index (should be IFINDEX_INTERNAL for non-kernel or
  70. deleted interfaces). */
  71. unsigned int ifindex;
  72. #define IFINDEX_INTERNAL 0
  73. /* Zebra internal interface status */
  74. u_char status;
  75. #define ZEBRA_INTERFACE_ACTIVE (1 << 0)
  76. #define ZEBRA_INTERFACE_SUB (1 << 1)
  77. #define ZEBRA_INTERFACE_LINKDETECTION (1 << 2)
  78. /* Interface flags. */
  79. uint64_t flags;
  80. /* Interface metric */
  81. int metric;
  82. /* Interface MTU. */
  83. unsigned int mtu; /* IPv4 MTU */
  84. unsigned int mtu6; /* IPv6 MTU - probably, but not neccessarily same as mtu */
  85. /* Hardware address. */
  86. #ifdef HAVE_SOCKADDR_DL
  87. struct sockaddr_dl sdl;
  88. #else
  89. unsigned short hw_type;
  90. u_char hw_addr[INTERFACE_HWADDR_MAX];
  91. int hw_addr_len;
  92. #endif /* HAVE_SOCKADDR_DL */
  93. /* interface bandwidth, kbits */
  94. unsigned int bandwidth;
  95. /* description of the interface. */
  96. char *desc;
  97. /* Distribute list. */
  98. void *distribute_in;
  99. void *distribute_out;
  100. /* Connected address list. */
  101. struct list *connected;
  102. /* Daemon specific interface data pointer. */
  103. void *info;
  104. /* Statistics fileds. */
  105. #ifdef HAVE_PROC_NET_DEV
  106. struct if_stats stats;
  107. #endif /* HAVE_PROC_NET_DEV */
  108. #ifdef HAVE_NET_RT_IFLIST
  109. struct if_data stats;
  110. #endif /* HAVE_NET_RT_IFLIST */
  111. };
  112. /* Connected address structure. */
  113. struct connected
  114. {
  115. /* Attached interface. */
  116. struct interface *ifp;
  117. /* Flags for configuration. */
  118. u_char conf;
  119. #define ZEBRA_IFC_REAL (1 << 0)
  120. #define ZEBRA_IFC_CONFIGURED (1 << 1)
  121. /*
  122. The ZEBRA_IFC_REAL flag should be set if and only if this address
  123. exists in the kernel.
  124. The ZEBRA_IFC_CONFIGURED flag should be set if and only if this address
  125. was configured by the user from inside quagga.
  126. */
  127. /* Flags for connected address. */
  128. u_char flags;
  129. #define ZEBRA_IFA_SECONDARY (1 << 0)
  130. #define ZEBRA_IFA_PEER (1 << 1)
  131. /* N.B. the ZEBRA_IFA_PEER flag should be set if and only if
  132. a peer address has been configured. If this flag is set,
  133. the destination field must contain the peer address.
  134. Otherwise, if this flag is not set, the destination address
  135. will either contain a broadcast address or be NULL.
  136. */
  137. /* Address of connected network. */
  138. struct prefix *address;
  139. /* Peer or Broadcast address, depending on whether ZEBRA_IFA_PEER is set.
  140. Note: destination may be NULL if ZEBRA_IFA_PEER is not set. */
  141. struct prefix *destination;
  142. /* Label for Linux 2.2.X and upper. */
  143. char *label;
  144. };
  145. /* Does the destination field contain a peer address? */
  146. #define CONNECTED_PEER(C) CHECK_FLAG((C)->flags, ZEBRA_IFA_PEER)
  147. /* Prefix to insert into the RIB */
  148. #define CONNECTED_PREFIX(C) \
  149. (CONNECTED_PEER(C) ? (C)->destination : (C)->address)
  150. /* Identifying address. We guess that if there's a peer address, but the
  151. local address is in the same prefix, then the local address may be unique. */
  152. #define CONNECTED_ID(C) \
  153. ((CONNECTED_PEER(C) && !prefix_match((C)->destination, (C)->address)) ?\
  154. (C)->destination : (C)->address)
  155. /* Interface hook sort. */
  156. #define IF_NEW_HOOK 0
  157. #define IF_DELETE_HOOK 1
  158. /* There are some interface flags which are only supported by some
  159. operating system. */
  160. #ifndef IFF_NOTRAILERS
  161. #define IFF_NOTRAILERS 0x0
  162. #endif /* IFF_NOTRAILERS */
  163. #ifndef IFF_OACTIVE
  164. #define IFF_OACTIVE 0x0
  165. #endif /* IFF_OACTIVE */
  166. #ifndef IFF_SIMPLEX
  167. #define IFF_SIMPLEX 0x0
  168. #endif /* IFF_SIMPLEX */
  169. #ifndef IFF_LINK0
  170. #define IFF_LINK0 0x0
  171. #endif /* IFF_LINK0 */
  172. #ifndef IFF_LINK1
  173. #define IFF_LINK1 0x0
  174. #endif /* IFF_LINK1 */
  175. #ifndef IFF_LINK2
  176. #define IFF_LINK2 0x0
  177. #endif /* IFF_LINK2 */
  178. #ifndef IFF_NOXMIT
  179. #define IFF_NOXMIT 0x0
  180. #endif /* IFF_NOXMIT */
  181. #ifndef IFF_NORTEXCH
  182. #define IFF_NORTEXCH 0x0
  183. #endif /* IFF_NORTEXCH */
  184. #ifndef IFF_IPV4
  185. #define IFF_IPV4 0x0
  186. #endif /* IFF_IPV4 */
  187. #ifndef IFF_IPV6
  188. #define IFF_IPV6 0x0
  189. #endif /* IFF_IPV6 */
  190. #ifndef IFF_VIRTUAL
  191. #define IFF_VIRTUAL 0x0
  192. #endif /* IFF_VIRTUAL */
  193. /* Prototypes. */
  194. extern int if_cmp_func (struct interface *, struct interface *);
  195. extern struct interface *if_create (const char *name, int namelen);
  196. extern struct interface *if_lookup_by_index (unsigned int);
  197. extern struct interface *if_lookup_exact_address (struct in_addr);
  198. extern struct interface *if_lookup_address (struct in_addr);
  199. /* These 2 functions are to be used when the ifname argument is terminated
  200. by a '\0' character: */
  201. extern struct interface *if_lookup_by_name (const char *ifname);
  202. extern struct interface *if_get_by_name (const char *ifname);
  203. /* For these 2 functions, the namelen argument should be the precise length
  204. of the ifname string (not counting any optional trailing '\0' character).
  205. In most cases, strnlen should be used to calculate the namelen value. */
  206. extern struct interface *if_lookup_by_name_len(const char *ifname,
  207. size_t namelen);
  208. extern struct interface *if_get_by_name_len(const char *ifname, size_t namelen);
  209. /* Delete the interface, but do not free the structure, and leave it in the
  210. interface list. It is often advisable to leave the pseudo interface
  211. structure because there may be configuration information attached. */
  212. extern void if_delete_retain (struct interface *);
  213. /* Delete and free the interface structure: calls if_delete_retain and then
  214. deletes it from the interface list and frees the structure. */
  215. extern void if_delete (struct interface *);
  216. extern int if_is_up (struct interface *);
  217. extern int if_is_running (struct interface *);
  218. extern int if_is_operative (struct interface *);
  219. extern int if_is_loopback (struct interface *);
  220. extern int if_is_broadcast (struct interface *);
  221. extern int if_is_pointopoint (struct interface *);
  222. extern int if_is_multicast (struct interface *);
  223. extern void if_add_hook (int, int (*)(struct interface *));
  224. extern void if_init (void);
  225. extern void if_dump_all (void);
  226. extern const char *if_flag_dump(unsigned long);
  227. /* Please use ifindex2ifname instead of if_indextoname where possible;
  228. ifindex2ifname uses internal interface info, whereas if_indextoname must
  229. make a system call. */
  230. extern const char *ifindex2ifname (unsigned int);
  231. /* Please use ifname2ifindex instead of if_nametoindex where possible;
  232. ifname2ifindex uses internal interface info, whereas if_nametoindex must
  233. make a system call. */
  234. extern unsigned int ifname2ifindex(const char *ifname);
  235. /* Connected address functions. */
  236. extern struct connected *connected_new (void);
  237. extern void connected_free (struct connected *);
  238. extern void connected_add (struct interface *, struct connected *);
  239. extern struct connected *connected_add_by_prefix (struct interface *,
  240. struct prefix *,
  241. struct prefix *);
  242. extern struct connected *connected_delete_by_prefix (struct interface *,
  243. struct prefix *);
  244. extern struct connected *connected_lookup_address (struct interface *,
  245. struct in_addr);
  246. #ifndef HAVE_IF_NAMETOINDEX
  247. extern unsigned int if_nametoindex (const char *);
  248. #endif
  249. #ifndef HAVE_IF_INDEXTONAME
  250. extern char *if_indextoname (unsigned int, char *);
  251. #endif
  252. /* Exported variables. */
  253. extern struct list *iflist;
  254. extern struct cmd_element interface_desc_cmd;
  255. extern struct cmd_element no_interface_desc_cmd;
  256. extern struct cmd_element interface_cmd;
  257. extern struct cmd_element no_interface_cmd;
  258. extern struct cmd_element interface_pseudo_cmd;
  259. extern struct cmd_element no_interface_pseudo_cmd;
  260. extern struct cmd_element show_address_cmd;
  261. #endif /* _ZEBRA_IF_H */