if.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "zebra.h"
  19. #include "linklist.h"
  20. /* Interface link-layer type, if known. Derived from:
  21. *
  22. * net/if_arp.h on various platforms - Linux especially.
  23. * http://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml
  24. *
  25. * Some of the more obviously defunct technologies left out.
  26. */
  27. enum zebra_link_type {
  28. ZEBRA_LLT_UNKNOWN = 0,
  29. ZEBRA_LLT_ETHER,
  30. ZEBRA_LLT_EETHER,
  31. ZEBRA_LLT_AX25,
  32. ZEBRA_LLT_PRONET,
  33. ZEBRA_LLT_IEEE802,
  34. ZEBRA_LLT_ARCNET,
  35. ZEBRA_LLT_APPLETLK,
  36. ZEBRA_LLT_DLCI,
  37. ZEBRA_LLT_ATM,
  38. ZEBRA_LLT_METRICOM,
  39. ZEBRA_LLT_IEEE1394,
  40. ZEBRA_LLT_EUI64,
  41. ZEBRA_LLT_INFINIBAND,
  42. ZEBRA_LLT_SLIP,
  43. ZEBRA_LLT_CSLIP,
  44. ZEBRA_LLT_SLIP6,
  45. ZEBRA_LLT_CSLIP6,
  46. ZEBRA_LLT_RSRVD,
  47. ZEBRA_LLT_ADAPT,
  48. ZEBRA_LLT_ROSE,
  49. ZEBRA_LLT_X25,
  50. ZEBRA_LLT_PPP,
  51. ZEBRA_LLT_CHDLC,
  52. ZEBRA_LLT_LAPB,
  53. ZEBRA_LLT_RAWHDLC,
  54. ZEBRA_LLT_IPIP,
  55. ZEBRA_LLT_IPIP6,
  56. ZEBRA_LLT_FRAD,
  57. ZEBRA_LLT_SKIP,
  58. ZEBRA_LLT_LOOPBACK,
  59. ZEBRA_LLT_LOCALTLK,
  60. ZEBRA_LLT_FDDI,
  61. ZEBRA_LLT_SIT,
  62. ZEBRA_LLT_IPDDP,
  63. ZEBRA_LLT_IPGRE,
  64. ZEBRA_LLT_IP6GRE,
  65. ZEBRA_LLT_PIMREG,
  66. ZEBRA_LLT_HIPPI,
  67. ZEBRA_LLT_ECONET,
  68. ZEBRA_LLT_IRDA,
  69. ZEBRA_LLT_FCPP,
  70. ZEBRA_LLT_FCAL,
  71. ZEBRA_LLT_FCPL,
  72. ZEBRA_LLT_FCFABRIC,
  73. ZEBRA_LLT_IEEE802_TR,
  74. ZEBRA_LLT_IEEE80211,
  75. ZEBRA_LLT_IEEE80211_RADIOTAP,
  76. ZEBRA_LLT_IEEE802154,
  77. ZEBRA_LLT_IEEE802154_PHY,
  78. };
  79. /*
  80. Interface name length.
  81. Linux define value in /usr/include/linux/if.h.
  82. #define IFNAMSIZ 16
  83. FreeBSD define value in /usr/include/net/if.h.
  84. #define IFNAMSIZ 16
  85. */
  86. #define INTERFACE_NAMSIZ 20
  87. #define INTERFACE_HWADDR_MAX 20
  88. typedef signed int ifindex_t;
  89. #ifdef HAVE_PROC_NET_DEV
  90. struct if_stats
  91. {
  92. unsigned long rx_packets; /* total packets received */
  93. unsigned long tx_packets; /* total packets transmitted */
  94. unsigned long rx_bytes; /* total bytes received */
  95. unsigned long tx_bytes; /* total bytes transmitted */
  96. unsigned long rx_errors; /* bad packets received */
  97. unsigned long tx_errors; /* packet transmit problems */
  98. unsigned long rx_dropped; /* no space in linux buffers */
  99. unsigned long tx_dropped; /* no space available in linux */
  100. unsigned long rx_multicast; /* multicast packets received */
  101. unsigned long rx_compressed;
  102. unsigned long tx_compressed;
  103. unsigned long collisions;
  104. /* detailed rx_errors: */
  105. unsigned long rx_length_errors;
  106. unsigned long rx_over_errors; /* receiver ring buff overflow */
  107. unsigned long rx_crc_errors; /* recved pkt with crc error */
  108. unsigned long rx_frame_errors; /* recv'd frame alignment error */
  109. unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  110. unsigned long rx_missed_errors; /* receiver missed packet */
  111. /* detailed tx_errors */
  112. unsigned long tx_aborted_errors;
  113. unsigned long tx_carrier_errors;
  114. unsigned long tx_fifo_errors;
  115. unsigned long tx_heartbeat_errors;
  116. unsigned long tx_window_errors;
  117. };
  118. #endif /* HAVE_PROC_NET_DEV */
  119. /* Interface structure */
  120. struct interface
  121. {
  122. /* Interface name. This should probably never be changed after the
  123. interface is created, because the configuration info for this interface
  124. is associated with this structure. For that reason, the interface
  125. should also never be deleted (to avoid losing configuration info).
  126. To delete, just set ifindex to IFINDEX_INTERNAL to indicate that the
  127. interface does not exist in the kernel.
  128. */
  129. char name[INTERFACE_NAMSIZ + 1];
  130. /* Interface index (should be IFINDEX_INTERNAL for non-kernel or
  131. deleted interfaces). */
  132. ifindex_t ifindex;
  133. #define IFINDEX_INTERNAL 0
  134. /* Zebra internal interface status */
  135. u_char status;
  136. #define ZEBRA_INTERFACE_ACTIVE (1 << 0)
  137. #define ZEBRA_INTERFACE_SUB (1 << 1)
  138. #define ZEBRA_INTERFACE_LINKDETECTION (1 << 2)
  139. /* Interface flags. */
  140. uint64_t flags;
  141. /* Interface metric */
  142. int metric;
  143. /* Interface MTU. */
  144. unsigned int mtu; /* IPv4 MTU */
  145. unsigned int mtu6; /* IPv6 MTU - probably, but not neccessarily same as mtu */
  146. /* Link-layer information and hardware address */
  147. enum zebra_link_type ll_type;
  148. u_char hw_addr[INTERFACE_HWADDR_MAX];
  149. int hw_addr_len;
  150. /* interface bandwidth, kbits */
  151. unsigned int bandwidth;
  152. /* description of the interface. */
  153. char *desc;
  154. /* Distribute list. */
  155. void *distribute_in;
  156. void *distribute_out;
  157. /* Connected address list. */
  158. struct list *connected;
  159. /* Daemon specific interface data pointer. */
  160. void *info;
  161. /* Statistics fileds. */
  162. #ifdef HAVE_PROC_NET_DEV
  163. struct if_stats stats;
  164. #endif /* HAVE_PROC_NET_DEV */
  165. #ifdef HAVE_NET_RT_IFLIST
  166. struct if_data stats;
  167. #endif /* HAVE_NET_RT_IFLIST */
  168. vrf_id_t vrf_id;
  169. };
  170. /* Connected address structure. */
  171. struct connected
  172. {
  173. /* Attached interface. */
  174. struct interface *ifp;
  175. /* Flags for configuration. */
  176. u_char conf;
  177. #define ZEBRA_IFC_REAL (1 << 0)
  178. #define ZEBRA_IFC_CONFIGURED (1 << 1)
  179. #define ZEBRA_IFC_QUEUED (1 << 2)
  180. /*
  181. The ZEBRA_IFC_REAL flag should be set if and only if this address
  182. exists in the kernel and is actually usable. (A case where it exists but
  183. is not yet usable would be IPv6 with DAD)
  184. The ZEBRA_IFC_CONFIGURED flag should be set if and only if this address
  185. was configured by the user from inside quagga.
  186. The ZEBRA_IFC_QUEUED flag should be set if and only if the address exists
  187. in the kernel. It may and should be set although the address might not be
  188. usable yet. (compare with ZEBRA_IFC_REAL)
  189. */
  190. /* Flags for connected address. */
  191. u_char flags;
  192. #define ZEBRA_IFA_SECONDARY (1 << 0)
  193. #define ZEBRA_IFA_PEER (1 << 1)
  194. /* N.B. the ZEBRA_IFA_PEER flag should be set if and only if
  195. a peer address has been configured. If this flag is set,
  196. the destination field must contain the peer address.
  197. Otherwise, if this flag is not set, the destination address
  198. will either contain a broadcast address or be NULL.
  199. */
  200. /* Address of connected network. */
  201. struct prefix *address;
  202. /* Peer or Broadcast address, depending on whether ZEBRA_IFA_PEER is set.
  203. Note: destination may be NULL if ZEBRA_IFA_PEER is not set. */
  204. struct prefix *destination;
  205. /* Label for Linux 2.2.X and upper. */
  206. char *label;
  207. };
  208. /* Does the destination field contain a peer address? */
  209. #define CONNECTED_PEER(C) CHECK_FLAG((C)->flags, ZEBRA_IFA_PEER)
  210. /* Prefix to insert into the RIB */
  211. #define CONNECTED_PREFIX(C) \
  212. (CONNECTED_PEER(C) ? (C)->destination : (C)->address)
  213. /* Identifying address. We guess that if there's a peer address, but the
  214. local address is in the same prefix, then the local address may be unique. */
  215. #define CONNECTED_ID(C) \
  216. ((CONNECTED_PEER(C) && !prefix_match((C)->destination, (C)->address)) ?\
  217. (C)->destination : (C)->address)
  218. /* Interface hook sort. */
  219. #define IF_NEW_HOOK 0
  220. #define IF_DELETE_HOOK 1
  221. /* There are some interface flags which are only supported by some
  222. operating system. */
  223. #ifndef IFF_NOTRAILERS
  224. #define IFF_NOTRAILERS 0x0
  225. #endif /* IFF_NOTRAILERS */
  226. #ifndef IFF_OACTIVE
  227. #define IFF_OACTIVE 0x0
  228. #endif /* IFF_OACTIVE */
  229. #ifndef IFF_SIMPLEX
  230. #define IFF_SIMPLEX 0x0
  231. #endif /* IFF_SIMPLEX */
  232. #ifndef IFF_LINK0
  233. #define IFF_LINK0 0x0
  234. #endif /* IFF_LINK0 */
  235. #ifndef IFF_LINK1
  236. #define IFF_LINK1 0x0
  237. #endif /* IFF_LINK1 */
  238. #ifndef IFF_LINK2
  239. #define IFF_LINK2 0x0
  240. #endif /* IFF_LINK2 */
  241. #ifndef IFF_NOXMIT
  242. #define IFF_NOXMIT 0x0
  243. #endif /* IFF_NOXMIT */
  244. #ifndef IFF_NORTEXCH
  245. #define IFF_NORTEXCH 0x0
  246. #endif /* IFF_NORTEXCH */
  247. #ifndef IFF_IPV4
  248. #define IFF_IPV4 0x0
  249. #endif /* IFF_IPV4 */
  250. #ifndef IFF_IPV6
  251. #define IFF_IPV6 0x0
  252. #endif /* IFF_IPV6 */
  253. #ifndef IFF_VIRTUAL
  254. #define IFF_VIRTUAL 0x0
  255. #endif /* IFF_VIRTUAL */
  256. /* Prototypes. */
  257. extern int if_cmp_func (struct interface *, struct interface *);
  258. extern struct interface *if_create (const char *name, int namelen);
  259. extern struct interface *if_lookup_by_index (ifindex_t);
  260. extern struct interface *if_lookup_exact_address (struct in_addr);
  261. extern struct interface *if_lookup_address (struct in_addr);
  262. extern struct interface *if_lookup_prefix (struct prefix *prefix);
  263. extern struct interface *if_create_vrf (const char *name, int namelen,
  264. vrf_id_t vrf_id);
  265. extern struct interface *if_lookup_by_index_vrf (ifindex_t, vrf_id_t vrf_id);
  266. extern struct interface *if_lookup_exact_address_vrf (struct in_addr,
  267. vrf_id_t vrf_id);
  268. extern struct interface *if_lookup_address_vrf (struct in_addr,
  269. vrf_id_t vrf_id);
  270. extern struct interface *if_lookup_prefix_vrf (struct prefix *prefix,
  271. vrf_id_t vrf_id);
  272. /* These 2 functions are to be used when the ifname argument is terminated
  273. by a '\0' character: */
  274. extern struct interface *if_lookup_by_name (const char *ifname);
  275. extern struct interface *if_get_by_name (const char *ifname);
  276. extern struct interface *if_lookup_by_name_vrf (const char *ifname,
  277. vrf_id_t vrf_id);
  278. extern struct interface *if_get_by_name_vrf (const char *ifname,
  279. vrf_id_t vrf_id);
  280. /* For these 2 functions, the namelen argument should be the precise length
  281. of the ifname string (not counting any optional trailing '\0' character).
  282. In most cases, strnlen should be used to calculate the namelen value. */
  283. extern struct interface *if_lookup_by_name_len(const char *ifname,
  284. size_t namelen);
  285. extern struct interface *if_get_by_name_len(const char *ifname,size_t namelen);
  286. extern struct interface *if_lookup_by_name_len_vrf(const char *ifname,
  287. size_t namelen, vrf_id_t vrf_id);
  288. extern struct interface *if_get_by_name_len_vrf(const char *ifname,
  289. size_t namelen, vrf_id_t vrf_id);
  290. /* Delete the interface, but do not free the structure, and leave it in the
  291. interface list. It is often advisable to leave the pseudo interface
  292. structure because there may be configuration information attached. */
  293. extern void if_delete_retain (struct interface *);
  294. /* Delete and free the interface structure: calls if_delete_retain and then
  295. deletes it from the interface list and frees the structure. */
  296. extern void if_delete (struct interface *);
  297. extern int if_is_up (struct interface *);
  298. extern int if_is_running (struct interface *);
  299. extern int if_is_operative (struct interface *);
  300. extern int if_is_loopback (struct interface *);
  301. extern int if_is_broadcast (struct interface *);
  302. extern int if_is_pointopoint (struct interface *);
  303. extern int if_is_multicast (struct interface *);
  304. extern void if_add_hook (int, int (*)(struct interface *));
  305. extern void if_init (vrf_id_t, struct list **);
  306. extern void if_terminate (vrf_id_t, struct list **);
  307. extern void if_dump_all (void);
  308. extern const char *if_flag_dump(unsigned long);
  309. extern const char *if_link_type_str (enum zebra_link_type);
  310. /* Please use ifindex2ifname instead of if_indextoname where possible;
  311. ifindex2ifname uses internal interface info, whereas if_indextoname must
  312. make a system call. */
  313. extern const char *ifindex2ifname (ifindex_t);
  314. extern const char *ifindex2ifname_vrf (ifindex_t, vrf_id_t vrf_id);
  315. /* Please use ifname2ifindex instead of if_nametoindex where possible;
  316. ifname2ifindex uses internal interface info, whereas if_nametoindex must
  317. make a system call. */
  318. extern ifindex_t ifname2ifindex(const char *ifname);
  319. extern ifindex_t ifname2ifindex_vrf(const char *ifname, vrf_id_t vrf_id);
  320. /* Connected address functions. */
  321. extern struct connected *connected_new (void);
  322. extern void connected_free (struct connected *);
  323. extern void connected_add (struct interface *, struct connected *);
  324. extern struct connected *connected_add_by_prefix (struct interface *,
  325. struct prefix *,
  326. struct prefix *);
  327. extern struct connected *connected_delete_by_prefix (struct interface *,
  328. struct prefix *);
  329. extern struct connected *connected_lookup_address (struct interface *,
  330. struct in_addr);
  331. #ifndef HAVE_IF_NAMETOINDEX
  332. extern ifindex_t if_nametoindex (const char *);
  333. #endif
  334. #ifndef HAVE_IF_INDEXTONAME
  335. extern char *if_indextoname (ifindex_t, char *);
  336. #endif
  337. /* Exported variables. */
  338. extern struct list *iflist;
  339. extern struct cmd_element interface_desc_cmd;
  340. extern struct cmd_element no_interface_desc_cmd;
  341. extern struct cmd_element interface_cmd;
  342. extern struct cmd_element no_interface_cmd;
  343. extern struct cmd_element interface_vrf_cmd;
  344. extern struct cmd_element no_interface_vrf_cmd;
  345. extern struct cmd_element interface_pseudo_cmd;
  346. extern struct cmd_element no_interface_pseudo_cmd;
  347. extern struct cmd_element show_address_cmd;
  348. extern struct cmd_element show_address_vrf_cmd;
  349. extern struct cmd_element show_address_vrf_all_cmd;
  350. #endif /* _ZEBRA_IF_H */