Add hardware support for InfiniBand (Debian Bug #294252 Tom Duffy from Sun).
[platform/upstream/net-tools.git] / lib / hw.c
1 /*
2  * lib/hw.c   This file contains the top-level part of the hardware
3  *              support functions module.
4  *
5  * Version:     $Id: hw.c,v 1.19 2008/10/03 01:52:04 ecki Exp $
6  *
7  * Maintainer:  Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
8  *
9  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10  *              Copyright 1993 MicroWalt Corporation
11  *
12  *980701 {1.21}  Arnaldo C. Melo       GNU gettext instead of catgets
13  *
14  *              This program is free software; you can redistribute it
15  *              and/or  modify it under  the terms of  the GNU General
16  *              Public  License as  published  by  the  Free  Software
17  *              Foundation;  either  version 2 of the License, or  (at
18  *              your option) any later version.
19  */
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/if_arp.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include "config.h"
30 #include "net-support.h"
31 #include "pathnames.h"
32 #include "intl.h"
33
34 extern struct hwtype unspec_hwtype;
35 extern struct hwtype loop_hwtype;
36
37 extern struct hwtype slip_hwtype;
38 extern struct hwtype cslip_hwtype;
39 extern struct hwtype slip6_hwtype;
40 extern struct hwtype cslip6_hwtype;
41 extern struct hwtype adaptive_hwtype;
42 extern struct hwtype strip_hwtype;
43
44 extern struct hwtype ether_hwtype;
45 extern struct hwtype fddi_hwtype;
46 extern struct hwtype hippi_hwtype;
47 extern struct hwtype tr_hwtype;
48 #ifdef ARPHRD_IEEE802_TR
49 extern struct hwtype tr_hwtype1;
50 #endif
51
52 extern struct hwtype ax25_hwtype;
53 extern struct hwtype rose_hwtype;
54 extern struct hwtype netrom_hwtype;
55 extern struct hwtype x25_hwtype;
56 extern struct hwtype tunnel_hwtype;
57
58 extern struct hwtype ash_hwtype;
59
60 extern struct hwtype ppp_hwtype;
61
62 extern struct hwtype arcnet_hwtype;
63
64 extern struct hwtype dlci_hwtype;
65 extern struct hwtype frad_hwtype;
66
67 extern struct hwtype hdlc_hwtype;
68 extern struct hwtype lapb_hwtype;
69
70 extern struct hwtype sit_hwtype;
71
72 extern struct hwtype irda_hwtype;
73
74 extern struct hwtype ec_hwtype;
75
76 extern struct hwtype ib_hwtype;
77
78 extern struct hwtype eui64_hwtype;
79
80 static struct hwtype *hwtypes[] =
81 {
82
83     &loop_hwtype,
84
85 #if HAVE_HWSLIP
86     &slip_hwtype,
87     &cslip_hwtype,
88     &slip6_hwtype,
89     &cslip6_hwtype,
90     &adaptive_hwtype,
91 #endif
92 #if HAVE_HWSTRIP
93     &strip_hwtype,
94 #endif
95 #if HAVE_HWASH
96     &ash_hwtype,
97 #endif
98 #if HAVE_HWETHER
99     &ether_hwtype,
100 #endif
101 #if HAVE_HWTR
102     &tr_hwtype,
103 #ifdef ARPHRD_IEEE802_TR
104     &tr_hwtype1, 
105 #endif
106 #endif
107 #if HAVE_HWAX25
108     &ax25_hwtype,
109 #endif
110 #if HAVE_HWNETROM
111     &netrom_hwtype,
112 #endif
113 #if HAVE_HWROSE
114     &rose_hwtype,
115 #endif
116 #if HAVE_HWTUNNEL
117     &tunnel_hwtype,
118 #endif
119 #if HAVE_HWPPP
120     &ppp_hwtype,
121 #endif
122 #if HAVE_HWHDLCLAPB
123     &hdlc_hwtype,
124     &lapb_hwtype,
125 #endif
126 #if HAVE_HWARC
127     &arcnet_hwtype,
128 #endif
129 #if HAVE_HWFR
130     &dlci_hwtype,
131     &frad_hwtype,
132 #endif
133 #if HAVE_HWSIT
134     &sit_hwtype,
135 #endif
136 #if HAVE_HWFDDI
137     &fddi_hwtype,
138 #endif
139 #if HAVE_HWHIPPI
140     &hippi_hwtype,
141 #endif
142 #if HAVE_HWIRDA
143     &irda_hwtype,
144 #endif
145 #if HAVE_HWEC
146     &ec_hwtype,
147 #endif
148 #if HAVE_HWX25
149     &x25_hwtype,
150 #endif
151 #if HAVE_HWIB
152     &ib_hwtype,
153 #endif
154 #if HAVE_HWEUI64
155     &eui64_hwtype,
156 #endif
157     &unspec_hwtype,
158     NULL
159 };
160
161 static short sVhwinit = 0;
162
163 void hwinit()
164 {
165     loop_hwtype.title = _("Local Loopback");
166     unspec_hwtype.title = _("UNSPEC");
167 #if HAVE_HWSLIP
168     slip_hwtype.title = _("Serial Line IP");
169     cslip_hwtype.title = _("VJ Serial Line IP");
170     slip6_hwtype.title = _("6-bit Serial Line IP");
171     cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
172     adaptive_hwtype.title = _("Adaptive Serial Line IP");
173 #endif
174 #if HAVE_HWETHER
175     ether_hwtype.title = _("Ethernet");
176 #endif
177 #if HAVE_HWASH
178     ash_hwtype.title = _("Ash");
179 #endif
180 #if HAVE_HWFDDI
181     fddi_hwtype.title = _("Fiber Distributed Data Interface");
182 #endif
183 #if HAVE_HWHIPPI
184     hippi_hwtype.title = _("HIPPI");
185 #endif
186 #if HAVE_HWAX25
187     ax25_hwtype.title = _("AMPR AX.25");
188 #endif
189 #if HAVE_HWROSE
190     rose_hwtype.title = _("AMPR ROSE");
191 #endif
192 #if HAVE_HWNETROM
193     netrom_hwtype.title = _("AMPR NET/ROM");
194 #endif
195 #if HAVE_HWX25
196     x25_hwtype.title = _("generic X.25");
197 #endif
198 #if HAVE_HWTUNNEL
199     tunnel_hwtype.title = _("IPIP Tunnel");
200 #endif
201 #if HAVE_HWPPP
202     ppp_hwtype.title = _("Point-to-Point Protocol");
203 #endif
204 #if HAVE_HWHDLCLAPB
205     hdlc_hwtype.title = _("(Cisco)-HDLC");
206     lapb_hwtype.title = _("LAPB");
207 #endif
208 #if HAVE_HWARC
209     arcnet_hwtype.title = _("ARCnet");
210 #endif
211 #if HAVE_HWFR
212     dlci_hwtype.title = _("Frame Relay DLCI");
213     frad_hwtype.title = _("Frame Relay Access Device");
214 #endif
215 #if HAVE_HWSIT
216     sit_hwtype.title = _("IPv6-in-IPv4");
217 #endif
218 #if HAVE_HWIRDA
219     irda_hwtype.title = _("IrLAP");
220 #endif
221 #if HAVE_HWTR
222     tr_hwtype.title = _("16/4 Mbps Token Ring");
223 #ifdef ARPHRD_IEEE802_TR
224     tr_hwtype1.title = _("16/4 Mbps Token Ring (New)") ; 
225 #endif
226 #endif
227 #if HAVE_HWEC
228     ec_hwtype.title = _("Econet");
229 #endif
230 #if HAVE_HWIB
231     ib_hwtype.title = _("InfiniBand");
232 #endif
233 #if HAVE_HWEUI64
234     eui64_hwtype.title = _("Generic EUI-64");
235 #endif
236     sVhwinit = 1;
237 }
238
239 /* Check our hardware type table for this type. */
240 struct hwtype *get_hwtype(const char *name)
241 {
242     struct hwtype **hwp;
243
244     if (!sVhwinit)
245         hwinit();
246
247     hwp = hwtypes;
248     while (*hwp != NULL) {
249         if (!strcmp((*hwp)->name, name))
250             return (*hwp);
251         hwp++;
252     }
253     return (NULL);
254 }
255
256
257 /* Check our hardware type table for this type. */
258 struct hwtype *get_hwntype(int type)
259 {
260     struct hwtype **hwp;
261
262     if (!sVhwinit)
263         hwinit();
264
265     hwp = hwtypes;
266     while (*hwp != NULL) {
267         if ((*hwp)->type == type)
268             return (*hwp);
269         hwp++;
270     }
271     return (NULL);
272 }
273
274 /* type: 0=all, 1=ARPable */
275 void print_hwlist(int type) {
276     int count = 0;
277     char * txt;
278     struct hwtype **hwp;
279
280     if (!sVhwinit)
281         hwinit();
282
283     hwp = hwtypes;
284     while (*hwp != NULL) {
285         if (((type == 1) && ((*hwp)->alen == 0)) || ((*hwp)->type == -1)) {
286                 hwp++; continue;
287         }
288         if ((count % 3) == 0) fprintf(stderr,count?"\n    ":"    "); 
289         txt = (*hwp)->name; if (!txt) txt = "..";
290         fprintf(stderr,"%s (%s) ",txt,(*hwp)->title);
291         count++;
292         hwp++;
293     }
294     fprintf(stderr,"\n");
295 }
296
297 /* return 1 if address is all zeros */
298 int hw_null_address(struct hwtype *hw, void *ap)
299 {
300     unsigned int i;
301     unsigned char *address = (unsigned char *)ap;
302     for (i = 0; i < hw->alen; i++)
303         if (address[i])
304             return 0;
305     return 1;
306 }