summaryrefslogtreecommitdiff
path: root/utils/fwparam_ibft/fw_entry.c
blob: 9dbaf5956e6a2877c1351596939a9f6f7489f78c (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
/*
 * Copyright (C) IBM Corporation. 2007
 * Author: Doug Maxey <dwm@austin.ibm.com>
 * based on code written by "Prasanna Mumbai" <mumbai.prasanna@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>

#include "fw_context.h"
#include "fwparam.h"
#include "idbm_fields.h"
#include "iscsi_net_util.h"

/**
 * fw_setup_nics - setup nics (ethXs) based on ibft net info
 *
 * Currently does not support vlans.
 *
 * If this is a offload card, this function does nothing. The
 * net info is used by the iscsi iface settings for the iscsi
 * function.
 */
int fw_setup_nics(void)
{
	struct boot_context *context;
	struct list_head targets;
	char *iface_prev = NULL, transport[16];
	int sock;
	int ret;

	INIT_LIST_HEAD(&targets);

	ret = fw_get_targets(&targets);
	if (ret || list_empty(&targets)) {
		printf("Could not setup fw entries.\n");
		return ENODEV;
	}

	/* Create socket for making networking changes */
	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("socket(AF_INET, SOCK_DGRAM, 0)");
		ret = errno;
		goto free_targets;
	}

	/*
	 * For each target in iBFT bring up required NIC and use routing
	 * to force iSCSI traffic through correct NIC
	 */
	list_for_each_entry(context, &targets, list) {			
		if (!net_get_transport_name_from_iface(context->iface,
						        transport))
			continue;

		/* Bring up NIC with correct address  - unless it
		 * has already been handled (2 targets in IBFT may share
		 * one NIC)
		 */
		struct sockaddr_in ipaddr = { .sin_family = AF_INET };
		struct sockaddr_in netmask = { .sin_family = AF_INET };
		struct sockaddr_in hostmask = { .sin_family = AF_INET };
		struct sockaddr_in gateway = { .sin_family = AF_INET };
		struct sockaddr_in tgt_ipaddr = { .sin_family = AF_INET };
		struct rtentry rt;
		struct ifreq ifr;

		if (!strlen(context->iface)) {
			printf("No iface in fw entry\n");
			ret = EINVAL;
			continue;
		}
		if (!inet_aton(context->ipaddr, &ipaddr.sin_addr)) {
			printf("Invalid or no ipaddr in fw entry\n");
			ret = EINVAL;
			continue;
		}

		if (!inet_aton(context->mask, &netmask.sin_addr)) {
			printf("Invalid or no netmask in fw entry\n");
			ret = EINVAL;
			continue;
		}
		inet_aton("255.255.255.255", &hostmask.sin_addr);

		if (!inet_aton(context->target_ipaddr, &tgt_ipaddr.sin_addr)) {
			printf("Invalid or no target ipaddr in fw entry\n");
			ret = EINVAL;
			continue;
		}

		/* Only set IP/NM if this is a new interface */
		if (iface_prev == NULL || strcmp(context->iface, iface_prev)) {
			/* Note: test above works because there is a
 			 * maximum of two targets in the iBFT
 			 */
			iface_prev = context->iface;

			/* TODO: create vlan if strlen(context->vlan) */

			/* Bring up interface */
			memset(&ifr, 0, sizeof(ifr));
			strncpy(ifr.ifr_name, context->iface, IFNAMSIZ);
			ifr.ifr_flags = IFF_UP | IFF_RUNNING;
			if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
				perror("ioctl(SIOCSIFFLAGS)");
				ret = errno;
				continue;
			}
			/* Set IP address */
			memset(&ifr, 0, sizeof(ifr));
			strncpy(ifr.ifr_name, context->iface, IFNAMSIZ);
			memcpy(&ifr.ifr_addr, &ipaddr, sizeof(struct sockaddr));
			if (ioctl(sock, SIOCSIFADDR, &ifr) < 0) {
				perror("ioctl(SIOCSIFADDR)");
				ret = errno;
				continue;
			}
			/* Set netmask */
			memset(&ifr, 0, sizeof(ifr));
			strncpy(ifr.ifr_name, context->iface, IFNAMSIZ);
			memcpy(&ifr.ifr_addr, &netmask, sizeof(struct sockaddr));
			if (ioctl(sock, SIOCSIFNETMASK, &ifr) < 0) {
				perror("ioctl(SIOCSIFNETMASK)");
				ret = errno;
				continue;
			}
		}

		/* Set static route to target via this interface */
		memset((char *) &rt, 0, sizeof(rt));
		memcpy(&rt.rt_dst, &tgt_ipaddr, sizeof(tgt_ipaddr));
		memcpy(&rt.rt_genmask, &hostmask, sizeof(hostmask));
		rt.rt_flags = RTF_UP | RTF_HOST;
		rt.rt_dev = context->iface;

		if ((tgt_ipaddr.sin_addr.s_addr & netmask.sin_addr.s_addr) == 
			(ipaddr.sin_addr.s_addr & netmask.sin_addr.s_addr)) {
			/* Same subnet */
			if (ioctl(sock, SIOCADDRT, &rt) < 0) {
				if (errno != EEXIST) {
					perror("ioctl(SIOCADDRT)");
					ret = errno;
					continue;
				}
			}
		} else {
			/* Different subnet.  Use gateway */
			rt.rt_flags |= RTF_GATEWAY;
			if (!inet_aton(context->gateway, &gateway.sin_addr)) {
				printf("Invalid or no gateway in fw entry\n");
				ret = errno;
				continue;
			}
			memcpy(&rt.rt_gateway, &gateway, sizeof(gateway));
			if (ioctl(sock, SIOCADDRT, &rt) < 0) {
				if (errno != EEXIST) {
					perror("ioctl(SIOCADDRT)");
					ret = errno;
					continue;
				}
			}
		}
		/* This target handled */
	}

	close(sock);
free_targets:
	fw_free_targets(&targets);
	return ret;
}


/**
 * fw_get_entry - return boot context of portal used for boot
 * @context: firmware info of portal
 *
 * Returns non-zero if no portal was used for boot.
 *
 * This function is not thread safe.
 */
int fw_get_entry(struct boot_context *context)
{
	int ret;

	ret = fwparam_ppc_boot_info(context);
	if (ret)
		ret = fwparam_ibft_sysfs_boot_info(context);

	return ret;
}

/**
 * fw_get_targets - get a boot_context struct for each target
 * @list: list to add entires on.
 *
 * Returns zero if entries were found that can be traversed with the
 * list.h helpers, or non-zero if no entries are found.
 *
 * fw_free_targets should be called to free the list.
 *
 * This function is not thread safe.
 */
int fw_get_targets(struct list_head *list)
{
	int ret;

	ret = fwparam_ppc_get_targets(list);
	if (ret)
		ret = fwparam_ibft_sysfs_get_targets(list);

	return ret;
}

void fw_free_targets(struct list_head *list)
{
	struct boot_context *curr, *tmp;

	if (!list || list_empty(list))
		return;

	list_for_each_entry_safe(curr, tmp, list, list) {
		list_del(&curr->list);
		free(curr);
	}
}

static void dump_initiator(struct boot_context *context)
{
	if (strlen(context->initiatorname))
		printf("%s = %s\n", IFACE_INAME, context->initiatorname);

	if (strlen(context->isid))
		printf("%s = %s\n", IFACE_ISID, context->isid);
}

static void dump_target(struct boot_context *context)
{
	if (strlen(context->targetname))
		printf("%s = %s\n", NODE_NAME, context->targetname);

	if (strlen(context->target_ipaddr))
		printf(CONN_ADDR" = %s\n", 0, context->target_ipaddr);
	printf(CONN_PORT" = %d\n", 0, context->target_port);

	if (strlen(context->chap_name))
		printf("%s = %s\n", SESSION_USERNAME, context->chap_name);
	if (strlen(context->chap_password))
		printf("%s = %s\n", SESSION_PASSWORD, context->chap_password);
	if (strlen(context->chap_name_in))
		printf("%s = %s\n", SESSION_USERNAME_IN, context->chap_name_in);
	if (strlen(context->chap_password_in))
		printf("%s = %s\n", SESSION_PASSWORD_IN,
		       context->chap_password_in);

	if (strlen(context->lun))
		printf("%s = %s\n", NODE_BOOT_LUN, context->lun);
}

static void dump_network(struct boot_context *context)
{
	/* Dump the 8 byte mac address (not iser support) */
	if (strlen(context->mac))
		printf("%s = %s\n", IFACE_HWADDR, context->mac);
	/*
	 * If this has a valid address then DHCP was used (broadcom sends
	 * 0.0.0.0).
	 */
	if (strlen(context->dhcp) && strcmp(context->dhcp, "0.0.0.0"))
		printf("%s = DHCP\n", IFACE_BOOT_PROTO);
	else
		printf("%s = STATIC\n", IFACE_BOOT_PROTO);
	if (strlen(context->ipaddr))
		printf("%s = %s\n", IFACE_IPADDR, context->ipaddr);
	if (strlen(context->mask))
		printf("%s = %s\n", IFACE_SUBNET_MASK, context->mask);
	if (strlen(context->gateway))
		printf("%s = %s\n", IFACE_GATEWAY, context->gateway);
	if (strlen(context->primary_dns))
		printf("%s = %s\n", IFACE_PRIMARY_DNS, context->primary_dns);
	if (strlen(context->secondary_dns))
		printf("%s = %s\n", IFACE_SEC_DNS, context->secondary_dns);
	if (strlen(context->vlan))
		printf("%s = %s\n", IFACE_VLAN, context->vlan);
	if (strlen(context->iface))
		printf("%s = %s\n", IFACE_NETNAME, context->iface);
}

/**
 * fw_print_entry - print boot context info of portal used for boot
 * @context: firmware info of portal
 *
 * Does not print anything if no portal was used for boot.
 *
 * TODO: Merge this in with idbm.c helpers.
 */
void fw_print_entry(struct boot_context *context)
{
	printf("%s\n", ISCSI_BEGIN_REC);
	dump_initiator(context);
	dump_network(context);
	dump_target(context);
	printf("%s\n", ISCSI_END_REC);
}