Whamcloud - gitweb
LU-15074 build: Use strlcpy if strscpy is not available
[fs/lustre-release.git] / lnet / lnet / config.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/ctype.h>
35 #include <linux/inetdevice.h>
36 #include <linux/nsproxy.h>
37 #include <net/net_namespace.h>
38 #include <lnet/lib-lnet.h>
39
40 /* tmp struct for parsing routes */
41 struct lnet_text_buf {
42         struct list_head        ltb_list;       /* stash on lists */
43         int                     ltb_size;       /* allocated size */
44         char                    ltb_text[0];    /* text buffer */
45 };
46
47 static int lnet_tbnob = 0;                      /* track text buf allocation */
48 #define LNET_MAX_TEXTBUF_NOB     (64<<10)       /* bound allocation */
49 #define LNET_SINGLE_TEXTBUF_NOB  (4<<10)
50
51 #define SPACESTR " \t\v\r\n"
52 #define DELIMITERS ":()[]"
53
54 #ifndef HAVE_STRSCPY
55 #define strscpy(s1, s2, sz)     strlcpy((s1), (s2), (sz))
56 #endif
57
58 static void
59 lnet_syntax(const char *name, const char *str, int offset, int width)
60 {
61         static char dots[LNET_SINGLE_TEXTBUF_NOB];
62         static char dashes[LNET_SINGLE_TEXTBUF_NOB];
63
64         memset(dots, '.', sizeof(dots));
65         dots[sizeof(dots)-1] = 0;
66         memset(dashes, '-', sizeof(dashes));
67         dashes[sizeof(dashes)-1] = 0;
68
69         LCONSOLE_ERROR_MSG(0x10f, "Error parsing '%s=\"%s\"'\n", name, str);
70         LCONSOLE_ERROR_MSG(0x110, "here...........%.*s..%.*s|%.*s|\n",
71                            (int)strlen(name), dots, offset, dots,
72                             (width < 1) ? 0 : width - 1, dashes);
73 }
74
75 static int
76 lnet_issep (char c)
77 {
78         switch (c) {
79         case '\n':
80         case '\r':
81         case ';':
82                 return 1;
83         default:
84                 return 0;
85         }
86 }
87
88 bool
89 lnet_net_unique(__u32 net_id, struct list_head *netlist,
90                 struct lnet_net **net)
91 {
92         struct lnet_net  *net_l;
93
94         if (!netlist)
95                 return true;
96
97         list_for_each_entry(net_l, netlist, net_list) {
98                 if (net_l->net_id == net_id) {
99                         if (net != NULL)
100                                 *net = net_l;
101                         return false;
102                 }
103         }
104
105         return true;
106 }
107
108 /* check that the NI is unique within the list of NIs already added to
109  * a network */
110 bool
111 lnet_ni_unique_net(struct list_head *nilist, char *iface)
112 {
113         struct list_head *tmp;
114         struct lnet_ni *ni;
115
116         list_for_each(tmp, nilist) {
117                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
118
119                 if (ni->ni_interface != NULL &&
120                     strncmp(ni->ni_interface, iface, strlen(iface)) == 0)
121                         return false;
122         }
123
124         return true;
125 }
126 static bool
127 in_array(__u32 *array, __u32 size, __u32 value)
128 {
129         int i;
130
131         for (i = 0; i < size; i++) {
132                 if (array[i] == value)
133                         return false;
134         }
135
136         return true;
137 }
138
139 static int
140 lnet_net_append_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
141 {
142         __u32 *added_cpts = NULL;
143         int i, j = 0, rc = 0;
144
145         /*
146          * no need to go futher since a subset of the NIs already exist on
147          * all CPTs
148          */
149         if (net->net_ncpts == LNET_CPT_NUMBER)
150                 return 0;
151
152         if (cpts == NULL) {
153                 /* there is an NI which will exist on all CPTs */
154                 if (net->net_cpts != NULL)
155                         CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
156                 net->net_cpts = NULL;
157                 net->net_ncpts = LNET_CPT_NUMBER;
158                 return 0;
159         }
160
161         if (net->net_cpts == NULL) {
162                 CFS_ALLOC_PTR_ARRAY(net->net_cpts, ncpts);
163                 if (net->net_cpts == NULL)
164                         return -ENOMEM;
165                 memcpy(net->net_cpts, cpts, ncpts * sizeof(*net->net_cpts));
166                 net->net_ncpts = ncpts;
167                 return 0;
168         }
169
170         CFS_ALLOC_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
171         if (added_cpts == NULL)
172                 return -ENOMEM;
173
174         for (i = 0; i < ncpts; i++) {
175                 if (!in_array(net->net_cpts, net->net_ncpts, cpts[i])) {
176                         added_cpts[j] = cpts[i];
177                         j++;
178                 }
179         }
180
181         /* append the new cpts if any to the list of cpts in the net */
182         if (j > 0) {
183                 __u32 *array = NULL, *loc;
184                 __u32 total_entries = j + net->net_ncpts;
185
186                 CFS_ALLOC_PTR_ARRAY(array, total_entries);
187                 if (array == NULL) {
188                         rc = -ENOMEM;
189                         goto failed;
190                 }
191
192                 memcpy(array, net->net_cpts,
193                        net->net_ncpts * sizeof(*net->net_cpts));
194                 loc = array + net->net_ncpts;
195                 memcpy(loc, added_cpts, j * sizeof(*net->net_cpts));
196
197                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
198                 net->net_ncpts = total_entries;
199                 net->net_cpts = array;
200         }
201
202 failed:
203         CFS_FREE_PTR_ARRAY(added_cpts, LNET_CPT_NUMBER);
204
205         return rc;
206 }
207
208 static void
209 lnet_net_remove_cpts(__u32 *cpts, __u32 ncpts, struct lnet_net *net)
210 {
211         struct lnet_ni *ni;
212         int rc;
213
214         /*
215          * Operation Assumption:
216          *      This function is called after an NI has been removed from
217          *      its parent net.
218          *
219          * if we're removing an NI which exists on all CPTs then
220          * we have to check if any of the other NIs on this net also
221          * exists on all CPTs. If none, then we need to build our Net CPT
222          * list based on the remaining NIs.
223          *
224          * If the NI being removed exist on a subset of the CPTs then we
225          * alo rebuild the Net CPT list based on the remaining NIs, which
226          * should resutl in the expected Net CPT list.
227          */
228
229         /*
230          * sometimes this function can be called due to some failure
231          * creating an NI, before any of the cpts are allocated, so check
232          * for that case and don't do anything
233          */
234         if (ncpts == 0)
235                 return;
236
237         if (ncpts == LNET_CPT_NUMBER) {
238                 /*
239                  * first iteration through the NI list in the net to see
240                  * if any of the NIs exist on all the CPTs. If one is
241                  * found then our job is done.
242                  */
243                 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
244                         if (ni->ni_ncpts == LNET_CPT_NUMBER)
245                                 return;
246                 }
247         }
248
249         /*
250          * Rebuild the Net CPT list again, thereby only including only the
251          * CPTs which the remaining NIs are associated with.
252          */
253         if (net->net_cpts != NULL) {
254                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
255                 net->net_cpts = NULL;
256         }
257
258         list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
259                 rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts,
260                                           net);
261                 if (rc != 0) {
262                         CERROR("Out of Memory\n");
263                         /*
264                          * do our best to keep on going. Delete
265                          * the net cpts and set it to NULL. This
266                          * way we can keep on going but less
267                          * efficiently, since memory accesses might be
268                          * accross CPT lines.
269                          */
270                         if (net->net_cpts != NULL) {
271                                 CFS_FREE_PTR_ARRAY(net->net_cpts,
272                                                    net->net_ncpts);
273                                 net->net_cpts = NULL;
274                                 net->net_ncpts = LNET_CPT_NUMBER;
275                         }
276                         return;
277                 }
278         }
279 }
280
281 void
282 lnet_ni_free(struct lnet_ni *ni)
283 {
284         lnet_net_remove_cpts(ni->ni_cpts, ni->ni_ncpts, ni->ni_net);
285
286         if (ni->ni_refs != NULL)
287                 cfs_percpt_free(ni->ni_refs);
288
289         if (ni->ni_tx_queues != NULL)
290                 cfs_percpt_free(ni->ni_tx_queues);
291
292         if (ni->ni_cpts != NULL)
293                 cfs_expr_list_values_free(ni->ni_cpts, ni->ni_ncpts);
294
295         if (ni->ni_interface != NULL) {
296                 LIBCFS_FREE(ni->ni_interface,
297                             strlen(ni->ni_interface) + 1);
298         }
299
300         /* release reference to net namespace */
301         if (ni->ni_net_ns != NULL)
302                 put_net(ni->ni_net_ns);
303
304         LIBCFS_FREE(ni, sizeof(*ni));
305 }
306
307 void
308 lnet_net_free(struct lnet_net *net)
309 {
310         struct list_head *tmp, *tmp2;
311         struct lnet_ni *ni;
312
313         LASSERT(list_empty(&net->net_ni_zombie));
314
315         /*
316          * delete any nis that haven't been added yet. This could happen
317          * if there is a failure on net startup
318          */
319         list_for_each_safe(tmp, tmp2, &net->net_ni_added) {
320                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
321                 list_del_init(&ni->ni_netlist);
322                 lnet_ni_free(ni);
323         }
324
325         /* delete any nis which have been started. */
326         list_for_each_safe(tmp, tmp2, &net->net_ni_list) {
327                 ni = list_entry(tmp, struct lnet_ni, ni_netlist);
328                 list_del_init(&ni->ni_netlist);
329                 lnet_ni_free(ni);
330         }
331
332         if (net->net_cpts != NULL)
333                 CFS_FREE_PTR_ARRAY(net->net_cpts, net->net_ncpts);
334
335         LIBCFS_FREE(net, sizeof(*net));
336 }
337
338 struct lnet_net *
339 lnet_net_alloc(__u32 net_id, struct list_head *net_list)
340 {
341         struct lnet_net         *net;
342
343         if (!lnet_net_unique(net_id, net_list, &net)) {
344                 CDEBUG(D_NET, "Returning duplicate net %p %s\n", net,
345                        libcfs_net2str(net->net_id));
346                 return net;
347         }
348
349         LIBCFS_ALLOC(net, sizeof(*net));
350         if (net == NULL) {
351                 CERROR("Out of memory creating network %s\n",
352                        libcfs_net2str(net_id));
353                 return NULL;
354         }
355
356         INIT_LIST_HEAD(&net->net_list);
357         INIT_LIST_HEAD(&net->net_ni_list);
358         INIT_LIST_HEAD(&net->net_ni_added);
359         INIT_LIST_HEAD(&net->net_ni_zombie);
360         INIT_LIST_HEAD(&net->net_rtr_pref_nids);
361         spin_lock_init(&net->net_lock);
362
363         net->net_id = net_id;
364         net->net_last_alive = ktime_get_real_seconds();
365
366         net->net_sel_priority = LNET_MAX_SELECTION_PRIORITY;
367
368         /* initialize global paramters to undefiend */
369         net->net_tunables.lct_peer_timeout = -1;
370         net->net_tunables.lct_max_tx_credits = -1;
371         net->net_tunables.lct_peer_tx_credits = -1;
372         net->net_tunables.lct_peer_rtr_credits = -1;
373
374         if (net_list)
375                 list_add_tail(&net->net_list, net_list);
376
377         return net;
378 }
379
380 static int
381 lnet_ni_add_interface(struct lnet_ni *ni, char *iface)
382 {
383         size_t iface_len = strlen(iface) + 1;
384
385         if (ni == NULL)
386                 return -ENOMEM;
387
388         if (ni->ni_interface != NULL) {
389                 LCONSOLE_ERROR_MSG(0x115, "%s: interface %s already set for net %s: rc = %d\n",
390                                    iface, ni->ni_interface,
391                                    libcfs_net2str(LNET_NID_NET(&ni->ni_nid)),
392                                    -EINVAL);
393                 return -EINVAL;
394         }
395
396         /* Allocate memory for the interface, so the code parsing input into
397          * tokens and adding interfaces can free the input safely.
398          * ni->ni_interface is freed in lnet_ni_free().
399          */
400         LIBCFS_ALLOC(ni->ni_interface, iface_len);
401
402         if (ni->ni_interface == NULL) {
403                 CERROR("%s: cannot allocate net interface name: rc = %d\n",
404                         iface, -ENOMEM);
405                 return -ENOMEM;
406         }
407
408         strscpy(ni->ni_interface, iface, iface_len);
409
410         return 0;
411 }
412
413 static struct lnet_ni *
414 lnet_ni_alloc_common(struct lnet_net *net, char *iface)
415 {
416         struct lnet_tx_queue    *tq;
417         struct lnet_ni          *ni;
418         int                     i;
419
420         if (iface != NULL)
421                 /* make sure that this NI is unique in the net it's
422                  * being added to */
423                 if (!lnet_ni_unique_net(&net->net_ni_added, iface))
424                         return NULL;
425
426         LIBCFS_ALLOC(ni, sizeof(*ni));
427         if (ni == NULL) {
428                 CERROR("Out of memory creating network interface %s%s\n",
429                        libcfs_net2str(net->net_id),
430                        (iface != NULL) ? iface : "");
431                 return NULL;
432         }
433
434         spin_lock_init(&ni->ni_lock);
435         INIT_LIST_HEAD(&ni->ni_netlist);
436         INIT_LIST_HEAD(&ni->ni_recovery);
437         LNetInvalidateMDHandle(&ni->ni_ping_mdh);
438         ni->ni_refs = cfs_percpt_alloc(lnet_cpt_table(),
439                                        sizeof(*ni->ni_refs[0]));
440         if (ni->ni_refs == NULL)
441                 goto failed;
442
443         ni->ni_tx_queues = cfs_percpt_alloc(lnet_cpt_table(),
444                                             sizeof(*ni->ni_tx_queues[0]));
445         if (ni->ni_tx_queues == NULL)
446                 goto failed;
447
448         cfs_percpt_for_each(tq, i, ni->ni_tx_queues)
449                 INIT_LIST_HEAD(&tq->tq_delayed);
450
451         ni->ni_net = net;
452         /* LND will fill in the address part of the NID */
453         ni->ni_nid.nid_type = LNET_NETTYP(net->net_id);
454         ni->ni_nid.nid_num = cpu_to_be16(LNET_NETNUM(net->net_id));
455
456         /* Store net namespace in which current ni is being created */
457         if (current->nsproxy && current->nsproxy->net_ns)
458                 ni->ni_net_ns = get_net(current->nsproxy->net_ns);
459         else
460                 ni->ni_net_ns = get_net(&init_net);
461
462         ni->ni_state = LNET_NI_STATE_INIT;
463         ni->ni_sel_priority = LNET_MAX_SELECTION_PRIORITY;
464         list_add_tail(&ni->ni_netlist, &net->net_ni_added);
465
466         /*
467          * if an interface name is provided then make sure to add in that
468          * interface name in NI
469          */
470         if (iface)
471                 if (lnet_ni_add_interface(ni, iface) != 0)
472                         goto failed;
473
474         return ni;
475 failed:
476         lnet_ni_free(ni);
477         return NULL;
478 }
479
480 /* allocate and add to the provided network */
481 struct lnet_ni *
482 lnet_ni_alloc(struct lnet_net *net, struct cfs_expr_list *el, char *iface)
483 {
484         struct lnet_ni          *ni;
485         int                     rc;
486
487         ni = lnet_ni_alloc_common(net, iface);
488         if (!ni)
489                 return NULL;
490
491         if (!el) {
492                 ni->ni_cpts  = NULL;
493                 ni->ni_ncpts = LNET_CPT_NUMBER;
494         } else {
495                 rc = cfs_expr_list_values(el, LNET_CPT_NUMBER, &ni->ni_cpts);
496                 if (rc <= 0) {
497                         CERROR("Failed to set CPTs for NI %s(%s): %d\n",
498                                libcfs_net2str(net->net_id),
499                                (iface != NULL) ? iface : "", rc);
500                         goto failed;
501                 }
502
503                 LASSERT(rc <= LNET_CPT_NUMBER);
504                 if (rc == LNET_CPT_NUMBER) {
505                         CFS_FREE_PTR_ARRAY(ni->ni_cpts, rc);
506                         ni->ni_cpts = NULL;
507                 }
508
509                 ni->ni_ncpts = rc;
510         }
511
512         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
513         if (rc != 0)
514                 goto failed;
515
516         return ni;
517 failed:
518         lnet_ni_free(ni);
519         return NULL;
520 }
521
522 struct lnet_ni *
523 lnet_ni_alloc_w_cpt_array(struct lnet_net *net, __u32 *cpts, __u32 ncpts,
524                           char *iface)
525 {
526         struct lnet_ni          *ni;
527         int                     rc;
528
529         ni = lnet_ni_alloc_common(net, iface);
530         if (!ni)
531                 return NULL;
532
533         if (ncpts == 0) {
534                 ni->ni_cpts  = NULL;
535                 ni->ni_ncpts = LNET_CPT_NUMBER;
536         } else {
537                 size_t array_size = ncpts * sizeof(ni->ni_cpts[0]);
538
539                 CFS_ALLOC_PTR_ARRAY(ni->ni_cpts, ncpts);
540                 if (ni->ni_cpts == NULL)
541                         goto failed;
542                 memcpy(ni->ni_cpts, cpts, array_size);
543                 ni->ni_ncpts = ncpts;
544         }
545
546         rc = lnet_net_append_cpts(ni->ni_cpts, ni->ni_ncpts, net);
547         if (rc != 0)
548                 goto failed;
549
550         return ni;
551 failed:
552         lnet_ni_free(ni);
553         return NULL;
554 }
555
556 /*
557  * Parse the networks string and create the matching set of NIs on the
558  * nilist.
559  */
560 int
561 lnet_parse_networks(struct list_head *netlist, const char *networks)
562 {
563         struct cfs_expr_list *net_el = NULL;
564         struct cfs_expr_list *ni_el = NULL;
565         int             tokensize;
566         char            *tokens;
567         char            *str;
568         struct lnet_net *net;
569         struct lnet_ni  *ni = NULL;
570         __u32           net_id;
571         int             nnets = 0;
572
573         if (networks == NULL) {
574                 CERROR("networks string is undefined\n");
575                 return -EINVAL;
576         }
577
578         if (strlen(networks) > LNET_SINGLE_TEXTBUF_NOB) {
579                 /* _WAY_ conservative */
580                 LCONSOLE_ERROR_MSG(0x112, "Can't parse networks: string too "
581                                    "long\n");
582                 return -EINVAL;
583         }
584
585         tokensize = strlen(networks) + 1;
586
587         LIBCFS_ALLOC(tokens, tokensize);
588         if (tokens == NULL) {
589                 CERROR("Can't allocate net tokens\n");
590                 return -ENOMEM;
591         }
592
593         memcpy(tokens, networks, tokensize);
594         str = tokens;
595
596         /*
597          * Main parser loop.
598          *
599          * NB we don't check interface conflicts here; it's the LNDs
600          * responsibility (if it cares at all)
601          */
602         do {
603                 char *nistr;
604                 char *elstr;
605                 char *name;
606                 int rc;
607
608                 /*
609                  * Parse a network string into its components.
610                  *
611                  * <name>{"("...")"}{"["<el>"]"}
612                  */
613
614                 /* Network name (mandatory) */
615                 while (isspace(*str))
616                         *str++ = '\0';
617                 if (!*str)
618                         break;
619                 name = str;
620                 str += strcspn(str, SPACESTR ":()[],");
621                 while (isspace(*str))
622                         *str++ = '\0';
623
624                 /* Interface list (optional) */
625                 if (*str == '(') {
626                         *str++ = '\0';
627                         nistr = str;
628                         str += strcspn(str, ")");
629                         if (*str != ')') {
630                                 str = nistr;
631                                 goto failed_syntax;
632                         }
633                         do {
634                                 *str++ = '\0';
635                         } while (isspace(*str));
636                 } else {
637                         nistr = NULL;
638                 }
639
640                 /* CPT expression (optional) */
641                 if (*str == '[') {
642                         elstr = str;
643                         str += strcspn(str, "]");
644                         if (*str != ']') {
645                                 str = elstr;
646                                 goto failed_syntax;
647                         }
648                         rc = cfs_expr_list_parse(elstr, str - elstr + 1,
649                                                 0, LNET_CPT_NUMBER - 1,
650                                                 &net_el);
651                         if (rc != 0) {
652                                 str = elstr;
653                                 goto failed_syntax;
654                         }
655                         *elstr = '\0';
656                         do {
657                                 *str++ = '\0';
658                         } while (isspace(*str));
659                 }
660
661                 /* Bad delimiters */
662                 if (*str && (strchr(DELIMITERS, *str) != NULL))
663                         goto failed_syntax;
664
665                 /* go to the next net if it exits */
666                 str += strcspn(str, ",");
667                 if (*str == ',')
668                         *str++ = '\0';
669
670                 /*
671                  * At this point the name is properly terminated.
672                  */
673                 net_id = libcfs_str2net(name);
674                 if (net_id == LNET_NET_ANY) {
675                         LCONSOLE_ERROR_MSG(0x113,
676                                         "Unrecognised network type\n");
677                         str = name;
678                         goto failed_syntax;
679                 }
680
681                 if (LNET_NETTYP(net_id) == LOLND) {
682                         /* Loopback is implicit, and there can be only one. */
683                         if (net_el) {
684                                 cfs_expr_list_free(net_el);
685                                 net_el = NULL;
686                         }
687                         /* Should we error out instead? */
688                         continue;
689                 }
690
691                 /*
692                  * All network paramaters are now known.
693                  */
694                 nnets++;
695
696                 /* always allocate a net, since we will eventually add an
697                  * interface to it, or we will fail, in which case we'll
698                  * just delete it */
699                 net = lnet_net_alloc(net_id, netlist);
700                 if (IS_ERR_OR_NULL(net))
701                         goto failed;
702
703                 if (!nistr) {
704                         /*
705                          * No interface list was specified, allocate a
706                          * ni using the defaults.
707                          */
708                         ni = lnet_ni_alloc(net, net_el, NULL);
709                         if (IS_ERR_OR_NULL(ni))
710                                 goto failed;
711
712                         if (!nistr) {
713                                 if (net_el) {
714                                         cfs_expr_list_free(net_el);
715                                         net_el = NULL;
716                                 }
717                                 continue;
718                         }
719                 }
720
721                 do {
722                         elstr = NULL;
723
724                         /* Interface name (mandatory) */
725                         while (isspace(*nistr))
726                                 *nistr++ = '\0';
727                         name = nistr;
728                         nistr += strcspn(nistr, SPACESTR "[],");
729                         while (isspace(*nistr))
730                                 *nistr++ = '\0';
731
732                         /* CPT expression (optional) */
733                         if (*nistr == '[') {
734                                 elstr = nistr;
735                                 nistr += strcspn(nistr, "]");
736                                 if (*nistr != ']') {
737                                         str = elstr;
738                                         goto failed_syntax;
739                                 }
740                                 rc = cfs_expr_list_parse(elstr,
741                                                         nistr - elstr + 1,
742                                                         0, LNET_CPT_NUMBER - 1,
743                                                         &ni_el);
744                                 if (rc != 0) {
745                                         str = elstr;
746                                         goto failed_syntax;
747                                 }
748                                 *elstr = '\0';
749                                 do {
750                                         *nistr++ = '\0';
751                                 } while (isspace(*nistr));
752                         } else {
753                                 ni_el = net_el;
754                         }
755
756                         /*
757                          * End of single interface specificaton,
758                          * advance to the start of the next one, if
759                          * any.
760                          */
761                         if (*nistr == ',') {
762                                 do {
763                                         *nistr++ = '\0';
764                                 } while (isspace(*nistr));
765                                 if (!*nistr) {
766                                         str = nistr;
767                                         goto failed_syntax;
768                                 }
769                         } else if (*nistr) {
770                                 str = nistr;
771                                 goto failed_syntax;
772                         }
773
774                         /*
775                          * At this point the name is properly terminated.
776                          */
777                         if (!*name) {
778                                 str = name;
779                                 goto failed_syntax;
780                         }
781
782                         ni = lnet_ni_alloc(net, ni_el, name);
783                         if (IS_ERR_OR_NULL(ni))
784                                 goto failed;
785
786                         if (ni_el) {
787                                 if (ni_el != net_el) {
788                                         cfs_expr_list_free(ni_el);
789                                         ni_el = NULL;
790                                 }
791                         }
792                 } while (*nistr);
793
794                 if (net_el) {
795                         cfs_expr_list_free(net_el);
796                         net_el = NULL;
797                 }
798         } while (*str);
799
800         LIBCFS_FREE(tokens, tokensize);
801         return nnets;
802
803  failed_syntax:
804         lnet_syntax("networks", networks, (int)(str - tokens), strlen(str));
805  failed:
806         /* free the net list and all the nis on each net */
807         while (!list_empty(netlist)) {
808                 net = list_entry(netlist->next, struct lnet_net, net_list);
809
810                 list_del_init(&net->net_list);
811                 lnet_net_free(net);
812         }
813
814         if (ni_el && ni_el != net_el)
815                 cfs_expr_list_free(ni_el);
816         if (net_el)
817                 cfs_expr_list_free(net_el);
818
819         LIBCFS_FREE(tokens, tokensize);
820
821         return -EINVAL;
822 }
823
824 static struct lnet_text_buf *lnet_new_text_buf(int str_len)
825 {
826         struct lnet_text_buf *ltb;
827         int nob;
828
829         /* NB allocate space for the terminating 0 */
830         nob = offsetof(struct lnet_text_buf, ltb_text[str_len + 1]);
831         if (nob > LNET_SINGLE_TEXTBUF_NOB) {
832                 /* _way_ conservative for "route net gateway..." */
833                 CERROR("text buffer too big\n");
834                 return NULL;
835         }
836
837         if (lnet_tbnob + nob > LNET_MAX_TEXTBUF_NOB) {
838                 CERROR("Too many text buffers\n");
839                 return NULL;
840         }
841
842         LIBCFS_ALLOC(ltb, nob);
843         if (ltb == NULL)
844                 return NULL;
845
846         ltb->ltb_size = nob;
847         ltb->ltb_text[0] = 0;
848         lnet_tbnob += nob;
849         return ltb;
850 }
851
852 static void
853 lnet_free_text_buf(struct lnet_text_buf *ltb)
854 {
855         lnet_tbnob -= ltb->ltb_size;
856         LIBCFS_FREE(ltb, ltb->ltb_size);
857 }
858
859 static void
860 lnet_free_text_bufs(struct list_head *tbs)
861 {
862         struct lnet_text_buf  *ltb;
863
864         while (!list_empty(tbs)) {
865                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
866
867                 list_del(&ltb->ltb_list);
868                 lnet_free_text_buf(ltb);
869         }
870 }
871
872 static int
873 lnet_str2tbs_sep(struct list_head *tbs, const char *str)
874 {
875         LIST_HEAD(pending);
876         const char *sep;
877         int nob;
878         int i;
879         struct lnet_text_buf *ltb;
880
881         /* Split 'str' into separate commands */
882         for (;;) {
883                 /* skip leading whitespace */
884                 while (isspace(*str))
885                         str++;
886
887                 /* scan for separator or comment */
888                 for (sep = str; *sep != 0; sep++)
889                         if (lnet_issep(*sep) || *sep == '#')
890                                 break;
891
892                 nob = (int)(sep - str);
893                 if (nob > 0) {
894                         ltb = lnet_new_text_buf(nob);
895                         if (ltb == NULL) {
896                                 lnet_free_text_bufs(&pending);
897                                 return -ENOMEM;
898                         }
899
900                         for (i = 0; i < nob; i++)
901                                 if (isspace(str[i]))
902                                         ltb->ltb_text[i] = ' ';
903                                 else
904                                         ltb->ltb_text[i] = str[i];
905
906                         ltb->ltb_text[nob] = 0;
907
908                         list_add_tail(&ltb->ltb_list, &pending);
909                 }
910
911                 if (*sep == '#') {
912                         /* scan for separator */
913                         do {
914                                 sep++;
915                         } while (*sep != 0 && !lnet_issep(*sep));
916                 }
917
918                 if (*sep == 0)
919                         break;
920
921                 str = sep + 1;
922         }
923
924         list_splice(&pending, tbs->prev);
925         return 0;
926 }
927
928 static int
929 lnet_expand1tb(struct list_head *list,
930                char *str, char *sep1, char *sep2,
931                char *item, int itemlen)
932 {
933         int              len1 = (int)(sep1 - str);
934         int              len2 = strlen(sep2 + 1);
935         struct lnet_text_buf *ltb;
936
937         LASSERT (*sep1 == '[');
938         LASSERT (*sep2 == ']');
939
940         ltb = lnet_new_text_buf(len1 + itemlen + len2);
941         if (ltb == NULL)
942                 return -ENOMEM;
943
944         memcpy(ltb->ltb_text, str, len1);
945         memcpy(&ltb->ltb_text[len1], item, itemlen);
946         memcpy(&ltb->ltb_text[len1+itemlen], sep2 + 1, len2);
947         ltb->ltb_text[len1 + itemlen + len2] = 0;
948
949         list_add_tail(&ltb->ltb_list, list);
950         return 0;
951 }
952
953 static int
954 lnet_str2tbs_expand(struct list_head *tbs, char *str)
955 {
956         char              num[16];
957         LIST_HEAD(pending);
958         char             *sep;
959         char             *sep2;
960         char             *parsed;
961         char             *enditem;
962         int               lo;
963         int               hi;
964         int               stride;
965         int               i;
966         int               nob;
967         int               scanned;
968
969         sep = strchr(str, '[');
970         if (sep == NULL)                        /* nothing to expand */
971                 return 0;
972
973         sep2 = strchr(sep, ']');
974         if (sep2 == NULL)
975                 goto failed;
976
977         for (parsed = sep; parsed < sep2; parsed = enditem) {
978
979                 enditem = ++parsed;
980                 while (enditem < sep2 && *enditem != ',')
981                         enditem++;
982
983                 if (enditem == parsed)          /* no empty items */
984                         goto failed;
985
986                 if (sscanf(parsed, "%d-%d/%d%n", &lo, &hi, &stride, &scanned) < 3) {
987
988                         if (sscanf(parsed, "%d-%d%n", &lo, &hi, &scanned) < 2) {
989
990                                 /* simple string enumeration */
991                                 if (lnet_expand1tb(&pending, str, sep, sep2,
992                                                    parsed, (int)(enditem - parsed)) != 0)
993                                         goto failed;
994
995                                 continue;
996                         }
997
998                         stride = 1;
999                 }
1000
1001                 /* range expansion */
1002
1003                 if (enditem != parsed + scanned) /* no trailing junk */
1004                         goto failed;
1005
1006                 if (hi < 0 || lo < 0 || stride < 0 || hi < lo ||
1007                     (hi - lo) % stride != 0)
1008                         goto failed;
1009
1010                 for (i = lo; i <= hi; i += stride) {
1011
1012                         snprintf(num, sizeof(num), "%d", i);
1013                         nob = strlen(num);
1014                         if (nob + 1 == sizeof(num))
1015                                 goto failed;
1016
1017                         if (lnet_expand1tb(&pending, str, sep, sep2,
1018                                            num, nob) != 0)
1019                                 goto failed;
1020                 }
1021         }
1022
1023         list_splice(&pending, tbs->prev);
1024         return 1;
1025
1026  failed:
1027         lnet_free_text_bufs(&pending);
1028         return -EINVAL;
1029 }
1030
1031 static int
1032 lnet_parse_hops (char *str, unsigned int *hops)
1033 {
1034         int     len = strlen(str);
1035         int     nob = len;
1036
1037         return (sscanf(str, "%u%n", hops, &nob) >= 1 &&
1038                 nob == len &&
1039                 *hops > 0 && *hops < 256);
1040 }
1041
1042 #define LNET_PRIORITY_SEPARATOR (':')
1043
1044 static int
1045 lnet_parse_priority(char *str, unsigned int *priority, char **token)
1046 {
1047         int   nob;
1048         char *sep;
1049         int   len;
1050
1051         sep = strchr(str, LNET_PRIORITY_SEPARATOR);
1052         if (sep == NULL) {
1053                 *priority = 0;
1054                 return 0;
1055         }
1056         len = strlen(sep + 1);
1057
1058         if ((sscanf((sep+1), "%u%n", priority, &nob) < 1) || (len != nob)) {
1059                 /* Update the caller's token pointer so it treats the found
1060                    priority as the token to report in the error message. */
1061                 *token += sep - str + 1;
1062                 return -EINVAL;
1063         }
1064
1065         CDEBUG(D_NET, "gateway %s, priority %d, nob %d\n", str, *priority, nob);
1066
1067         /*
1068          * Change priority separator to \0 to be able to parse NID
1069          */
1070         *sep = '\0';
1071         return 0;
1072 }
1073
1074 static int
1075 lnet_parse_route(char *str, int *im_a_router)
1076 {
1077         /* static scratch buffer OK (single threaded) */
1078         static char cmd[LNET_SINGLE_TEXTBUF_NOB];
1079
1080         LIST_HEAD(nets);
1081         LIST_HEAD(gateways);
1082         struct list_head *tmp1;
1083         struct list_head *tmp2;
1084         __u32 net;
1085         struct lnet_nid nid;
1086         struct lnet_text_buf  *ltb;
1087         int rc;
1088         char *sep;
1089         char *token = str;
1090         int ntokens = 0;
1091         int myrc = -1;
1092         __u32 hops;
1093         int got_hops = 0;
1094         unsigned int priority = 0;
1095
1096         /* save a copy of the string for error messages */
1097         strncpy(cmd, str, sizeof(cmd));
1098         cmd[sizeof(cmd) - 1] = '\0';
1099
1100         sep = str;
1101         for (;;) {
1102                 /* scan for token start */
1103                 while (isspace(*sep))
1104                         sep++;
1105                 if (*sep == 0) {
1106                         if (ntokens < (got_hops ? 3 : 2))
1107                                 goto token_error;
1108                         break;
1109                 }
1110
1111                 ntokens++;
1112                 token = sep++;
1113
1114                 /* scan for token end */
1115                 while (*sep != 0 && !isspace(*sep))
1116                         sep++;
1117                 if (*sep != 0)
1118                         *sep++ = 0;
1119
1120                 if (ntokens == 1) {
1121                         tmp2 = &nets;           /* expanding nets */
1122                 } else if (ntokens == 2 &&
1123                            lnet_parse_hops(token, &hops)) {
1124                         got_hops = 1;           /* got a hop count */
1125                         continue;
1126                 } else {
1127                         tmp2 = &gateways;       /* expanding gateways */
1128                 }
1129
1130                 ltb = lnet_new_text_buf(strlen(token));
1131                 if (ltb == NULL)
1132                         goto out;
1133
1134                 strcpy(ltb->ltb_text, token);
1135                 tmp1 = &ltb->ltb_list;
1136                 list_add_tail(tmp1, tmp2);
1137
1138                 while (tmp1 != tmp2) {
1139                         ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1140
1141                         rc = lnet_str2tbs_expand(tmp1->next, ltb->ltb_text);
1142                         if (rc < 0)
1143                                 goto token_error;
1144
1145                         tmp1 = tmp1->next;
1146
1147                         if (rc > 0) {           /* expanded! */
1148                                 list_del(&ltb->ltb_list);
1149                                 lnet_free_text_buf(ltb);
1150                                 continue;
1151                         }
1152
1153                         if (ntokens == 1) {
1154                                 net = libcfs_str2net(ltb->ltb_text);
1155                                 if (net == LNET_NET_ANY ||
1156                                     LNET_NETTYP(net) == LOLND)
1157                                         goto token_error;
1158                         } else {
1159                                 rc = lnet_parse_priority(ltb->ltb_text,
1160                                                          &priority, &token);
1161                                 if (rc < 0)
1162                                         goto token_error;
1163
1164                                 if (libcfs_strnid(&nid, ltb->ltb_text) != 0 ||
1165                                     nid_is_lo0(&nid))
1166                                         goto token_error;
1167                         }
1168                 }
1169         }
1170
1171         /* if there are no hops set then we want to flag this value as
1172          * unset since hops is an optional parameter */
1173         if (!got_hops)
1174                 hops = LNET_UNDEFINED_HOPS;
1175
1176         LASSERT(!list_empty(&nets));
1177         LASSERT(!list_empty(&gateways));
1178
1179         list_for_each(tmp1, &nets) {
1180                 ltb = list_entry(tmp1, struct lnet_text_buf, ltb_list);
1181                 net = libcfs_str2net(ltb->ltb_text);
1182                 LASSERT(net != LNET_NET_ANY);
1183
1184                 list_for_each(tmp2, &gateways) {
1185                         ltb = list_entry(tmp2, struct lnet_text_buf, ltb_list);
1186                         LASSERT(libcfs_strnid(&nid, ltb->ltb_text) == 0);
1187
1188                         if (lnet_islocalnid(&nid)) {
1189                                 *im_a_router = 1;
1190                                 continue;
1191                         }
1192
1193                         rc = lnet_add_route(net, hops, &nid, priority, 1);
1194                         if (rc != 0 && rc != -EEXIST && rc != -EHOSTUNREACH) {
1195                                 CERROR("Can't create route "
1196                                        "to %s via %s\n",
1197                                        libcfs_net2str(net),
1198                                        libcfs_nidstr(&nid));
1199                                 goto out;
1200                         }
1201                 }
1202         }
1203
1204         myrc = 0;
1205         goto out;
1206
1207 token_error:
1208         lnet_syntax("routes", cmd, (int)(token - str), strlen(token));
1209 out:
1210         lnet_free_text_bufs(&nets);
1211         lnet_free_text_bufs(&gateways);
1212         return myrc;
1213 }
1214
1215 static int
1216 lnet_parse_route_tbs(struct list_head *tbs, int *im_a_router)
1217 {
1218         struct lnet_text_buf   *ltb;
1219
1220         while (!list_empty(tbs)) {
1221                 ltb = list_entry(tbs->next, struct lnet_text_buf, ltb_list);
1222
1223                 if (lnet_parse_route(ltb->ltb_text, im_a_router) < 0) {
1224                         lnet_free_text_bufs(tbs);
1225                         return -EINVAL;
1226                 }
1227
1228                 list_del(&ltb->ltb_list);
1229                 lnet_free_text_buf(ltb);
1230         }
1231
1232         return 0;
1233 }
1234
1235 int
1236 lnet_parse_routes(const char *routes, int *im_a_router)
1237 {
1238         LIST_HEAD(tbs);
1239         int rc = 0;
1240
1241         *im_a_router = 0;
1242
1243         if (lnet_str2tbs_sep(&tbs, routes) < 0) {
1244                 CERROR("Error parsing routes\n");
1245                 rc = -EINVAL;
1246         } else {
1247                 rc = lnet_parse_route_tbs(&tbs, im_a_router);
1248         }
1249
1250         LASSERT (lnet_tbnob == 0);
1251         return rc;
1252 }
1253
1254 static int
1255 lnet_match_network_token(char *token, int len, __u32 *ipaddrs, int nip)
1256 {
1257         LIST_HEAD(list);
1258         int             rc;
1259         int             i;
1260
1261         rc = cfs_ip_addr_parse(token, len, &list);
1262         if (rc != 0)
1263                 return rc;
1264
1265         for (rc = i = 0; !rc && i < nip; i++)
1266                 rc = cfs_ip_addr_match(ipaddrs[i], &list);
1267
1268         cfs_expr_list_free_list(&list);
1269
1270         return rc;
1271 }
1272
1273 static int
1274 lnet_match_network_tokens(char *net_entry, __u32 *ipaddrs, int nip)
1275 {
1276         static char tokens[LNET_SINGLE_TEXTBUF_NOB];
1277
1278         int   matched = 0;
1279         int   ntokens = 0;
1280         int   len;
1281         char *net = NULL;
1282         char *sep;
1283         char *token;
1284         int   rc;
1285
1286         LASSERT(strlen(net_entry) < sizeof(tokens));
1287
1288         /* work on a copy of the string */
1289         strcpy(tokens, net_entry);
1290         sep = tokens;
1291         for (;;) {
1292                 /* scan for token start */
1293                 while (isspace(*sep))
1294                         sep++;
1295                 if (*sep == 0)
1296                         break;
1297
1298                 token = sep++;
1299
1300                 /* scan for token end */
1301                 while (*sep != 0 && !isspace(*sep))
1302                         sep++;
1303                 if (*sep != 0)
1304                         *sep++ = 0;
1305
1306                 if (ntokens++ == 0) {
1307                         net = token;
1308                         continue;
1309                 }
1310
1311                 len = strlen(token);
1312
1313                 rc = lnet_match_network_token(token, len, ipaddrs, nip);
1314                 if (rc < 0) {
1315                         lnet_syntax("ip2nets", net_entry,
1316                                     (int)(token - tokens), len);
1317                         return rc;
1318                 }
1319
1320                 matched |= (rc != 0);
1321         }
1322
1323         if (!matched)
1324                 return 0;
1325
1326         strcpy(net_entry, net);                 /* replace with matched net */
1327         return 1;
1328 }
1329
1330 static __u32
1331 lnet_netspec2net(char *netspec)
1332 {
1333         char   *bracket = strchr(netspec, '(');
1334         __u32   net;
1335
1336         if (bracket != NULL)
1337                 *bracket = 0;
1338
1339         net = libcfs_str2net(netspec);
1340
1341         if (bracket != NULL)
1342                 *bracket = '(';
1343
1344         return net;
1345 }
1346
1347 static int
1348 lnet_splitnets(char *source, struct list_head *nets)
1349 {
1350         int               offset = 0;
1351         int               offset2;
1352         int               len;
1353         struct lnet_text_buf  *tb;
1354         struct lnet_text_buf  *tb2;
1355         struct list_head *t;
1356         char             *sep;
1357         char             *bracket;
1358         __u32             net;
1359
1360         LASSERT(!list_empty(nets));
1361         LASSERT(nets->next == nets->prev);      /* single entry */
1362
1363         tb = list_entry(nets->next, struct lnet_text_buf, ltb_list);
1364
1365         for (;;) {
1366                 sep = strchr(tb->ltb_text, ',');
1367                 bracket = strchr(tb->ltb_text, '(');
1368
1369                 if (sep != NULL &&
1370                     bracket != NULL &&
1371                     bracket < sep) {
1372                         /* netspec lists interfaces... */
1373
1374                         offset2 = offset + (int)(bracket - tb->ltb_text);
1375                         len = strlen(bracket);
1376
1377                         bracket = strchr(bracket + 1, ')');
1378
1379                         if (bracket == NULL ||
1380                             !(bracket[1] == ',' || bracket[1] == 0)) {
1381                                 lnet_syntax("ip2nets", source, offset2, len);
1382                                 return -EINVAL;
1383                         }
1384
1385                         sep = (bracket[1] == 0) ? NULL : bracket + 1;
1386                 }
1387
1388                 if (sep != NULL)
1389                         *sep++ = 0;
1390
1391                 net = lnet_netspec2net(tb->ltb_text);
1392                 if (net == LNET_NET_ANY) {
1393                         lnet_syntax("ip2nets", source, offset,
1394                                     strlen(tb->ltb_text));
1395                         return -EINVAL;
1396                 }
1397
1398                 list_for_each(t, nets) {
1399                         tb2 = list_entry(t, struct lnet_text_buf, ltb_list);
1400
1401                         if (tb2 == tb)
1402                                 continue;
1403
1404                         if (net == lnet_netspec2net(tb2->ltb_text)) {
1405                                 /* duplicate network */
1406                                 lnet_syntax("ip2nets", source, offset,
1407                                             strlen(tb->ltb_text));
1408                                 return -EINVAL;
1409                         }
1410                 }
1411
1412                 if (sep == NULL)
1413                         return 0;
1414
1415                 offset += (int)(sep - tb->ltb_text);
1416                 len = strlen(sep);
1417                 tb2 = lnet_new_text_buf(len);
1418                 if (tb2 == NULL)
1419                         return -ENOMEM;
1420
1421                 strncpy(tb2->ltb_text, sep, len);
1422                 tb2->ltb_text[len] = '\0';
1423                 list_add_tail(&tb2->ltb_list, nets);
1424
1425                 tb = tb2;
1426         }
1427 }
1428
1429 static int
1430 lnet_match_networks(const char **networksp, const char *ip2nets,
1431                     __u32 *ipaddrs, int nip)
1432 {
1433         static char       networks[LNET_SINGLE_TEXTBUF_NOB];
1434         static char       source[LNET_SINGLE_TEXTBUF_NOB];
1435
1436         LIST_HEAD(raw_entries);
1437         LIST_HEAD(matched_nets);
1438         LIST_HEAD(current_nets);
1439         struct list_head *t;
1440         struct list_head *t2;
1441         struct lnet_text_buf  *tb;
1442         int               len;
1443         int               count;
1444         int               rc;
1445
1446         if (lnet_str2tbs_sep(&raw_entries, ip2nets) < 0) {
1447                 CERROR("Error parsing ip2nets\n");
1448                 LASSERT(lnet_tbnob == 0);
1449                 return -EINVAL;
1450         }
1451
1452         networks[0] = 0;
1453         count = 0;
1454         len = 0;
1455         rc = 0;
1456
1457         while (!list_empty(&raw_entries)) {
1458                 tb = list_entry(raw_entries.next, struct lnet_text_buf,
1459                                 ltb_list);
1460
1461                 strncpy(source, tb->ltb_text, sizeof(source));
1462                 source[sizeof(source) - 1] = '\0';
1463
1464                 /* replace ltb_text with the network(s) add on match */
1465                 rc = lnet_match_network_tokens(tb->ltb_text, ipaddrs, nip);
1466                 if (rc < 0)
1467                         break;
1468
1469                 list_del(&tb->ltb_list);
1470
1471                 if (rc == 0) {                  /* no match */
1472                         lnet_free_text_buf(tb);
1473                         continue;
1474                 }
1475
1476                 /* split into separate networks */
1477                 INIT_LIST_HEAD(&current_nets);
1478                 list_add(&tb->ltb_list, &current_nets);
1479                 rc = lnet_splitnets(source, &current_nets);
1480                 if (rc < 0)
1481                         break;
1482
1483                 list_for_each_safe(t, t2, &current_nets) {
1484                         tb = list_entry(t, struct lnet_text_buf, ltb_list);
1485
1486                         list_move_tail(&tb->ltb_list, &matched_nets);
1487
1488                         len += scnprintf(networks + len, sizeof(networks) - len,
1489                                          "%s%s", (len == 0) ? "" : ",",
1490                                          tb->ltb_text);
1491
1492                         if (len >= sizeof(networks)) {
1493                                 CERROR("Too many matched networks\n");
1494                                 rc = -E2BIG;
1495                                 goto out;
1496                         }
1497                 }
1498
1499                 count++;
1500         }
1501
1502  out:
1503         lnet_free_text_bufs(&raw_entries);
1504         lnet_free_text_bufs(&matched_nets);
1505         lnet_free_text_bufs(&current_nets);
1506         LASSERT(lnet_tbnob == 0);
1507
1508         if (rc < 0)
1509                 return rc;
1510
1511         *networksp = networks;
1512         return count;
1513 }
1514
1515 int lnet_inet_enumerate(struct lnet_inetdev **dev_list, struct net *ns)
1516 {
1517         struct lnet_inetdev *ifaces = NULL;
1518         struct net_device *dev;
1519         int nalloc = 0;
1520         int nip = 0;
1521         DECLARE_CONST_IN_IFADDR(ifa);
1522
1523         rtnl_lock();
1524         for_each_netdev(ns, dev) {
1525                 int flags = dev_get_flags(dev);
1526                 struct in_device *in_dev;
1527                 int node_id;
1528                 int cpt;
1529
1530                 if (flags & IFF_LOOPBACK) /* skip the loopback IF */
1531                         continue;
1532
1533                 if (!(flags & IFF_UP)) {
1534                         CWARN("lnet: Ignoring interface %s: it's down\n",
1535                               dev->name);
1536                         continue;
1537                 }
1538
1539                 in_dev = __in_dev_get_rtnl(dev);
1540                 if (!in_dev) {
1541                         CWARN("lnet: Interface %s has no IPv4 status.\n",
1542                               dev->name);
1543                         continue;
1544                 }
1545
1546                 node_id = dev_to_node(&dev->dev);
1547                 cpt = cfs_cpt_of_node(lnet_cpt_table(), node_id);
1548
1549                 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
1550                         if (nip >= nalloc) {
1551                                 struct lnet_inetdev *tmp;
1552
1553                                 nalloc += LNET_INTERFACES_NUM;
1554                                 tmp = krealloc(ifaces, nalloc * sizeof(*tmp),
1555                                                GFP_KERNEL);
1556                                 if (!tmp) {
1557                                         kfree(ifaces);
1558                                         ifaces = NULL;
1559                                         nip = -ENOMEM;
1560                                         goto unlock_rtnl;
1561                                 }
1562                                 ifaces = tmp;
1563                         }
1564
1565                         ifaces[nip].li_cpt = cpt;
1566                         ifaces[nip].li_flags = flags;
1567                         ifaces[nip].li_ipaddr = ntohl(ifa->ifa_local);
1568                         ifaces[nip].li_netmask = ntohl(ifa->ifa_mask);
1569                         strlcpy(ifaces[nip].li_name, ifa->ifa_label,
1570                                 sizeof(ifaces[nip].li_name));
1571                         nip++;
1572                 }
1573                 endfor_ifa(in_dev);
1574         }
1575 unlock_rtnl:
1576         rtnl_unlock();
1577
1578         if (nip == 0) {
1579                 CERROR("lnet: Can't find any usable interfaces, rc = -ENOENT\n");
1580                 nip = -ENOENT;
1581         }
1582
1583         *dev_list = ifaces;
1584         return nip;
1585 }
1586 EXPORT_SYMBOL(lnet_inet_enumerate);
1587
1588 int
1589 lnet_parse_ip2nets(const char **networksp, const char *ip2nets)
1590 {
1591         struct lnet_inetdev *ifaces = NULL;
1592         __u32     *ipaddrs = NULL;
1593         int nip;
1594         int        rc;
1595         int i;
1596
1597         if (current->nsproxy && current->nsproxy->net_ns)
1598                 nip = lnet_inet_enumerate(&ifaces, current->nsproxy->net_ns);
1599         else
1600                 nip = lnet_inet_enumerate(&ifaces, &init_net);
1601         if (nip < 0) {
1602                 if (nip != -ENOENT) {
1603                         LCONSOLE_ERROR_MSG(0x117,
1604                                            "Error %d enumerating local IP interfaces for ip2nets to match\n",
1605                                            nip);
1606                 } else {
1607                         LCONSOLE_ERROR_MSG(0x118,
1608                                            "No local IP interfaces for ip2nets to match\n");
1609                 }
1610                 return nip;
1611         }
1612
1613         CFS_ALLOC_PTR_ARRAY(ipaddrs, nip);
1614         if (!ipaddrs) {
1615                 rc = -ENOMEM;
1616                 CERROR("lnet: Can't allocate ipaddrs[%d], rc = %d\n",
1617                        nip, rc);
1618                 goto out_free_addrs;
1619         }
1620
1621         for (i = 0; i < nip; i++)
1622                 ipaddrs[i] = ifaces[i].li_ipaddr;
1623
1624         rc = lnet_match_networks(networksp, ip2nets, ipaddrs, nip);
1625         if (rc < 0) {
1626                 LCONSOLE_ERROR_MSG(0x119, "Error %d parsing ip2nets\n", rc);
1627         } else if (rc == 0) {
1628                 LCONSOLE_ERROR_MSG(0x11a, "ip2nets does not match "
1629                                    "any local IP interfaces\n");
1630                 rc = -ENOENT;
1631         }
1632         CFS_FREE_PTR_ARRAY(ipaddrs, nip);
1633 out_free_addrs:
1634         kfree(ifaces);
1635         return rc > 0 ? 0 : rc;
1636 }