aboutsummaryrefslogtreecommitdiff
blob: ac5fa837f29db771044d6f0c9e1f60e5f73240d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
From fbc0a6667fe7ca44a5185b00be2c0cbfae10d61d Mon Sep 17 00:00:00 2001
From: Alexey Shvetsov <alexxy@gentoo.org>
Date: Thu, 15 Mar 2012 23:18:21 +0400
Subject: [PATCH 2/3] lpf ib

---
 client/dhclient.c |   31 ++++++++
 common/bpf.c      |   39 +++++++++-
 common/dlpi.c     |    4 +-
 common/lpf.c      |  225 ++++++++++++++++++++++++++++++++++++++++++++---------
 common/socket.c   |    4 +-
 includes/dhcp.h   |    1 +
 includes/dhcpd.h  |    3 +-
 7 files changed, 266 insertions(+), 41 deletions(-)

diff --git a/client/dhclient.c b/client/dhclient.c
index f72e0da..138b563 100644
--- a/client/dhclient.c
+++ b/client/dhclient.c
@@ -100,6 +100,29 @@ static int check_domain_name_list(const char *ptr, size_t len, int dots);
 static int check_option_values(struct universe *universe, unsigned int opt,
 			       const char *ptr, size_t len);
 
+static void setup_ib_interface(struct interface_info *ip)
+{
+	struct group *g;
+
+	/* Set the broadcast flag */
+	ip->client->config->bootp_broadcast_always = 1;
+
+	/*
+	 * Find out if a dhcp-client-identifier option was specified either
+	 * in the config file or on the command line
+	 */
+	for (g = ip->client->config->on_transmission; g != NULL; g = g->next) {
+		if ((g->statements != NULL) &&
+		    (strcmp(g->statements->data.option->option->name,
+			    "dhcp-client-identifier") == 0)) {
+			return;
+		}
+	}
+
+	/* No client ID specified */
+	log_fatal("dhcp-client-identifier must be specified for InfiniBand");
+}
+
 int
 main(int argc, char **argv) {
 	int fd;
@@ -603,6 +626,14 @@ main(int argc, char **argv) {
 	} else
 		srandom(seed + cur_time + (unsigned)getpid());
 
+	/* Setup specific Infiniband options */
+	for (ip = interfaces; ip; ip = ip->next) {
+		if (ip->client &&
+		    (ip->hw_address.hbuf[0] == HTYPE_INFINIBAND)) {
+			setup_ib_interface(ip);
+		}
+	}
+
 	/* Start a configuration state machine for each interface. */
 #ifdef DHCPv6
 	if (local_family == AF_INET6) {
diff --git a/common/bpf.c b/common/bpf.c
index b0ef657..90289ec 100644
--- a/common/bpf.c
+++ b/common/bpf.c
@@ -116,7 +116,7 @@ int if_register_bpf (info)
 		log_fatal ("Can't attach interface %s to bpf device %s: %m",
 		       info -> name, filename);
 
-	get_hw_addr(info->name, &info->hw_address);
+	get_hw_addr(info);
 
 	return sock;
 }
@@ -198,11 +198,44 @@ struct bpf_insn dhcp_bpf_filter [] = {
 	BPF_STMT(BPF_RET+BPF_K, 0),
 };
 
+/* Packet filter program for DHCP over Infiniband.
+ *
+ * XXX
+ * Changes to the filter program may require changes to the constant offsets
+ * used in lpf_gen_filter_setup to patch the port in the BPF program!
+ * XXX
+ */
+struct bpf_insn dhcp_ib_bpf_filter [] = {
+	/* Packet filter for Infiniband */
+	/* Make sure it's a UDP packet... */
+	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 9),
+	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
+
+	/* Make sure this isn't a fragment... */
+	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 6),
+	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
+
+	/* Get the IP header length... */
+	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0),
+
+	/* Make sure it's to the right port... */
+	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 2),
+	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),
+
+	/* If we passed all the tests, ask for the whole packet. */
+	BPF_STMT(BPF_RET + BPF_K, (u_int)-1),
+
+	/* Otherwise, drop it. */
+	BPF_STMT(BPF_RET + BPF_K, 0),
+};
+
 #if defined (DEC_FDDI)
 struct bpf_insn *bpf_fddi_filter;
 #endif
 
 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
+int dhcp_ib_bpf_filter_len = sizeof dhcp_ib_bpf_filter / sizeof (struct bpf_insn);
+
 #if defined (HAVE_TR_SUPPORT)
 struct bpf_insn dhcp_bpf_tr_filter [] = {
         /* accept all token ring packets due to variable length header */
@@ -552,7 +585,9 @@ void maybe_setup_fallback ()
 }
 
 void
-get_hw_addr(const char *name, struct hardware *hw) {
+get_hw_addr(struct interface_info *info) {
+	struct hardware *hw = &info->hw_address;
+	char *name = info->name;
 	struct ifaddrs *ifa;
 	struct ifaddrs *p;
 	struct sockaddr_dl *sa;
diff --git a/common/dlpi.c b/common/dlpi.c
index 8f2c73d..619271f 100644
--- a/common/dlpi.c
+++ b/common/dlpi.c
@@ -1332,7 +1332,9 @@ void maybe_setup_fallback ()
 #endif /* USE_DLPI_SEND */
 
 void 
-get_hw_addr(const char *name, struct hardware *hw) {
+get_hw_addr(struct interface_info *info) {
+	struct hardware *hw = &info->hw_address;
+	char *name = info->name;
 	int sock, unit;
 	long buf[DLPI_MAXDLBUF];
         union DL_primitives *dlp;
diff --git a/common/lpf.c b/common/lpf.c
index 16eecc9..488df88 100644
--- a/common/lpf.c
+++ b/common/lpf.c
@@ -41,10 +41,20 @@
 #include "includes/netinet/udp.h"
 #include "includes/netinet/if_ether.h"
 #include <net/if.h>
+#include <ifaddrs.h>
 
 /* Reinitializes the specified interface after an address change.   This
    is not required for packet-filter APIs. */
 
+/* Default broadcast address for IPoIB */
+static unsigned char default_ib_bcast_addr[20] = {
+ 	0x00, 0xff, 0xff, 0xff,
+	0xff, 0x12, 0x40, 0x1b,
+	0x00, 0x00, 0x00, 0x00,
+	0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff
+};
+
 #ifdef USE_LPF_SEND
 void if_reinitialize_send (info)
 	struct interface_info *info;
@@ -67,11 +77,22 @@ int if_register_lpf (info)
 	struct interface_info *info;
 {
 	int sock;
+	int type;
+	int protocol;
 	struct sockaddr sa;
 
 	/* Make an LPF socket. */
-	if ((sock = socket(PF_PACKET, SOCK_PACKET,
-			   htons((short)ETH_P_ALL))) < 0) {
+	get_hw_addr(info);
+
+	if (info->hw_address.hbuf[0] == HTYPE_INFINIBAND) {
+		type     = SOCK_DGRAM;
+		protocol = ETHERTYPE_IP;
+	} else {
+		type     = SOCK_RAW;
+		protocol = ETH_P_ALL;
+	}
+
+	if ((sock = socket(PF_PACKET, type, htons((short)protocol))) < 0) {
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
 		    errno == EAFNOSUPPORT || errno == EINVAL) {
@@ -88,6 +109,7 @@ int if_register_lpf (info)
 	/* Bind to the interface name */
 	memset (&sa, 0, sizeof sa);
 	sa.sa_family = AF_PACKET;
+	sa.ll.sll_protocol = htons(protocol);
 	strncpy (sa.sa_data, (const char *)info -> ifp, sizeof sa.sa_data);
 	if (bind (sock, &sa, sizeof sa)) {
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
@@ -103,8 +125,6 @@ int if_register_lpf (info)
 		log_fatal ("Bind socket to interface: %m");
 	}
 
-	get_hw_addr(info->name, &info->hw_address);
-
 	return sock;
 }
 #endif /* USE_LPF_SEND || USE_LPF_RECEIVE */
@@ -159,6 +179,8 @@ void if_deregister_send (info)
    in bpf includes... */
 extern struct sock_filter dhcp_bpf_filter [];
 extern int dhcp_bpf_filter_len;
+extern struct sock_filter dhcp_ib_bpf_filter [];
+extern int dhcp_ib_bpf_filter_len;
 
 #if defined (HAVE_TR_SUPPORT)
 extern struct sock_filter dhcp_bpf_tr_filter [];
@@ -217,15 +239,28 @@ static void lpf_gen_filter_setup (info)
 
 	memset(&p, 0, sizeof(p));
 
-	/* Set up the bpf filter program structure.    This is defined in
-	   bpf.c */
-	p.len = dhcp_bpf_filter_len;
-	p.filter = dhcp_bpf_filter;
-
-        /* Patch the server port into the LPF  program...
-	   XXX changes to filter program may require changes
-	   to the insn number(s) used below! XXX */
-	dhcp_bpf_filter [8].k = ntohs ((short)local_port);
+	if (info->hw_address.hbuf[0] == HTYPE_INFINIBAND) {
+		/* Set up the bpf filter program structure. */
+		p.len = dhcp_ib_bpf_filter_len;
+		p.filter = dhcp_ib_bpf_filter;
+
+		/* Patch the server port into the LPF program...
+		   XXX
+		   changes to filter program may require changes
+		   to the insn number(s) used below!
+		   XXX */
+		dhcp_ib_bpf_filter[6].k = ntohs ((short)local_port);
+	} else {
+		/* Set up the bpf filter program structure.
+		   This is defined in bpf.c */
+		p.len = dhcp_bpf_filter_len;
+		p.filter = dhcp_bpf_filter;
+
+		/* Patch the server port into the LPF  program...
+		   XXX changes to filter program may require changes
+		   to the insn number(s) used below! XXX */
+		dhcp_bpf_filter [8].k = ntohs ((short)local_port);
+	}
 
 	if (setsockopt (info -> rfdesc, SOL_SOCKET, SO_ATTACH_FILTER, &p,
 			sizeof p) < 0) {
@@ -282,6 +317,54 @@ static void lpf_tr_filter_setup (info)
 #endif /* USE_LPF_RECEIVE */
 
 #ifdef USE_LPF_SEND
+ssize_t send_packet_ib(interface, packet, raw, len, from, to, hto)
+	struct interface_info *interface;
+	struct packet *packet;
+	struct dhcp_packet *raw;
+	size_t len;
+	struct in_addr from;
+	struct sockaddr_in *to;
+	struct hardware *hto;
+{
+	unsigned ibufp = 0;
+	double ih [1536 / sizeof (double)];
+	unsigned char *buf = (unsigned char *)ih;
+	ssize_t result;
+
+	union sockunion {
+		struct sockaddr sa;
+		struct sockaddr_ll sll;
+		struct sockaddr_storage ss;
+	} su;
+
+	assemble_udp_ip_header (interface, buf, &ibufp, from.s_addr,
+				to->sin_addr.s_addr, to->sin_port,
+				(unsigned char *)raw, len);
+	memcpy (buf + ibufp, raw, len);
+
+	memset(&su, 0, sizeof(su));
+	su.sll.sll_family = AF_PACKET;
+	su.sll.sll_protocol = htons(ETHERTYPE_IP);
+
+	if (!(su.sll.sll_ifindex = if_nametoindex(interface->name))) {
+		errno = ENOENT;
+		log_error ("send_packet_ib: %m - failed to get if index");
+		return -1;
+	}
+
+	su.sll.sll_hatype = htons(HTYPE_INFINIBAND);
+	su.sll.sll_halen = sizeof(interface->bcast_addr);
+	memcpy(&su.sll.sll_addr, interface->bcast_addr, 20);
+
+	result = sendto(interface->wfdesc, buf, ibufp + len, 0,
+			&su.sa, sizeof(su));
+
+	if (result < 0)
+		log_error ("send_packet_ib: %m");
+
+	return result;
+}
+
 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
 	struct interface_info *interface;
 	struct packet *packet;
@@ -303,6 +386,11 @@ ssize_t send_packet (interface, packet, raw, len, from, to, hto)
 		return send_fallback (interface, packet, raw,
 				      len, from, to, hto);
 
+	if (interface->hw_address.hbuf[0] == HTYPE_INFINIBAND) {
+		return send_packet_ib(interface, packet, raw, len, from,
+				      to, hto);
+	}
+
 	if (hto == NULL && interface->anycast_mac_addr.hlen)
 		hto = &interface->anycast_mac_addr;
 
@@ -334,6 +422,46 @@ ssize_t send_packet (interface, packet, raw, len, from, to, hto)
 #endif /* USE_LPF_SEND */
 
 #ifdef USE_LPF_RECEIVE
+ssize_t receive_packet_ib (interface, buf, len, from, hfrom)
+	struct interface_info *interface;
+	unsigned char *buf;
+	size_t len;
+	struct sockaddr_in *from;
+	struct hardware *hfrom;
+{
+	int length = 0;
+	int offset = 0;
+	unsigned char ibuf [1536];
+	unsigned bufix = 0;
+	unsigned paylen;
+
+	length = read(interface->rfdesc, ibuf, sizeof(ibuf));
+
+	if (interface->hw_address.hbuf[0] == HTYPE_INFINIBAND) {
+		return receive_packet_ib(interface, buf, len, from, hfrom);
+	}
+
+	if (length <= 0)
+		return length;
+
+	offset = decode_udp_ip_header(interface, ibuf, bufix, from,
+				       (unsigned)length, &paylen, 0);
+
+	if (offset < 0)
+		return 0;
+
+	bufix += offset;
+	length -= offset;
+
+	if (length < paylen)
+		log_fatal("Internal inconsistency at %s:%d.", MDL);
+
+	/* Copy out the data in the packet... */
+	memcpy(buf, &ibuf[bufix], paylen);
+
+	return (ssize_t)paylen;
+}
+
 ssize_t receive_packet (interface, buf, len, from, hfrom)
 	struct interface_info *interface;
 	unsigned char *buf;
@@ -419,33 +547,42 @@ void maybe_setup_fallback ()
 }
 
 void
-get_hw_addr(const char *name, struct hardware *hw) {
-	int sock;
-	struct ifreq tmp;
-	struct sockaddr *sa;
+get_hw_addr(struct interface_info *info)
+{
+	struct hardware *hw = &info->hw_address;
+	char *name = info->name;
+	struct ifaddrs *ifaddrs;
+	struct ifaddrs *ifa;
+	struct sockaddr_ll *sll = NULL;
 
-	if (strlen(name) >= sizeof(tmp.ifr_name)) {
-		log_fatal("Device name too long: \"%s\"", name);
-	}
+	if (getifaddrs(&ifaddrs) == -1)
+		log_fatal("Failed to get interfaces");
+
+	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
 
-	sock = socket(AF_INET, SOCK_DGRAM, 0);
-	if (sock < 0) {
-		log_fatal("Can't create socket for \"%s\": %m", name);
+		if (ifa->ifa_addr->sa_family != AF_PACKET)
+			continue;
+
+		if (ifa->ifa_flags & IFF_LOOPBACK)
+			continue;
+
+		if (strcmp(ifa->ifa_name, name) == 0) {
+			sll = (struct sockaddr_ll *)(void *)ifa->ifa_addr;
+			break;
+		}
 	}
 
-	memset(&tmp, 0, sizeof(tmp));
-	strcpy(tmp.ifr_name, name);
-	if (ioctl(sock, SIOCGIFHWADDR, &tmp) < 0) {
-		log_fatal("Error getting hardware address for \"%s\": %m", 
-			  name);
+	if (sll == NULL) {
+		freeifaddrs(ifaddrs);
+		log_fatal("Failed to get HW address for %s\n", name);
+	
 	}
 
-	sa = &tmp.ifr_hwaddr;
-	switch (sa->sa_family) {
+	switch (sll->sll_hatype) {
 		case ARPHRD_ETHER:
 			hw->hlen = 7;
 			hw->hbuf[0] = HTYPE_ETHER;
-			memcpy(&hw->hbuf[1], sa->sa_data, 6);
+			memcpy(&hw->hbuf[1], sll->sll_addr, 6);
 			break;
 		case ARPHRD_IEEE802:
 #ifdef ARPHRD_IEEE802_TR
@@ -453,18 +590,36 @@ get_hw_addr(const char *name, struct hardware *hw) {
 #endif /* ARPHRD_IEEE802_TR */
 			hw->hlen = 7;
 			hw->hbuf[0] = HTYPE_IEEE802;
-			memcpy(&hw->hbuf[1], sa->sa_data, 6);
+			memcpy(&hw->hbuf[1], sll->sll_addr, 6);
 			break;
 		case ARPHRD_FDDI:
 			hw->hlen = 17;
 			hw->hbuf[0] = HTYPE_FDDI;
-			memcpy(&hw->hbuf[1], sa->sa_data, 16);
+			memcpy(&hw->hbuf[1], sll->sll_addr, 16);
+			break;
+		case ARPHRD_INFINIBAND:
+			/* For Infiniband, save the broadcast address and store
+			 * the port GUID into the hardware address.
+			 */
+			if (ifa->ifa_flags & IFF_BROADCAST) {
+				struct sockaddr_ll *bll;
+
+				bll = (struct sockaddr_ll *)ifa->ifa_broadaddr;
+				memcpy(&info->bcast_addr, bll->sll_addr, 20);
+			} else {
+				memcpy(&info->bcast_addr, default_ib_bcast_addr,
+				       20);
+			}
+
+			hw->hlen = 1;
+			hw->hbuf[0] = HTYPE_INFINIBAND;
 			break;
 		default:
+			freeifaddrs(ifaddrs);
 			log_fatal("Unsupported device type %ld for \"%s\"",
-				  (long int)sa->sa_family, name);
+				  (long int)sll->sll_family, name);
 	}
 
-	close(sock);
+	freeifaddrs(ifaddrs);
 }
 #endif
diff --git a/common/socket.c b/common/socket.c
index a48404b..3fe8b75 100644
--- a/common/socket.c
+++ b/common/socket.c
@@ -324,7 +324,7 @@ void if_register_send (info)
 	info->wfdesc = if_register_socket(info, AF_INET, 0);
 	/* If this is a normal IPv4 address, get the hardware address. */
 	if (strcmp(info->name, "fallback") != 0)
-		get_hw_addr(info->name, &info->hw_address);
+		get_hw_addr(info);
 #if defined (USE_SOCKET_FALLBACK)
 	/* Fallback only registers for send, but may need to receive as
 	   well. */
@@ -497,7 +497,7 @@ if_register6(struct interface_info *info, int do_multicast) {
 	if (req_multi)
 		if_register_multicast(info);
 
-	get_hw_addr(info->name, &info->hw_address);
+	get_hw_addr(info);
 
 	if (!quiet_interface_discovery) {
 		if (info->shared_network != NULL) {
diff --git a/includes/dhcp.h b/includes/dhcp.h
index 1af2adf..722c661 100644
--- a/includes/dhcp.h
+++ b/includes/dhcp.h
@@ -79,6 +79,7 @@ struct dhcp_packet {
 #define HTYPE_ETHER	1               /* Ethernet 10Mbps              */
 #define HTYPE_IEEE802	6               /* IEEE 802.2 Token Ring...	*/
 #define HTYPE_FDDI	8		/* FDDI...			*/
+#define HTYPE_INFINIBAND 32		/* Infiniband IPoIB		*/
 
 /* Magic cookie validating dhcp options field (and bootp vendor
    extensions field). */
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
index 863a149..5a137e6 100644
--- a/includes/dhcpd.h
+++ b/includes/dhcpd.h
@@ -1239,6 +1239,7 @@ struct interface_info {
 	struct shared_network *shared_network;
 				/* Networks connected to this interface. */
 	struct hardware hw_address;	/* Its physical address. */
+	u_int8_t bcast_addr[20];	/* Infiniband broadcast address */
 	struct in_addr *addresses;	/* Addresses associated with this
 					 * interface.
 					 */
@@ -2355,7 +2356,7 @@ void print_dns_status (int, struct dhcp_ddns_cb *, isc_result_t);
 #endif
 const char *print_time(TIME);
 
-void get_hw_addr(const char *name, struct hardware *hw);
+void get_hw_addr(struct interface_info *info);
 
 /* socket.c */
 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_RECEIVE) \
-- 
1.7.9.3