blob: eca01b956c0b086ba9ebde011d4fb5760054c675 [file] [log] [blame]
Brad Bishop26bdd442019-08-16 17:08:17 -04001From 04eec97b390621f2b3794b0d774b77429eb88cfd Mon Sep 17 00:00:00 2001
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08002From: Andrea Adami <andrea.adami@gmail.com>
3Date: Wed, 2 May 2018 23:14:19 +0200
Brad Bishop26bdd442019-08-16 17:08:17 -04004Subject: [PATCH] add if_nameindex from musl
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08005
6Taken from musl, minimal changes.
7klibc lacks struct and func
8
9Fix
10
11 ifdown.o: In function `ifdown':
12 ifdown.c (.text+0x30): undefined reference to `if_nameindex'
13
14While there add klibc-specific guard and include sys/types.h
15to fix :
16
17 /kexec/if_nameindex.c:2:
18 /usr/lib/klibc/include/linux/types.h:22:0:
19 warning: "__bitwise" redefined
20 #define __bitwise __bitwise__
21
22Signed-off-by: Andrea Adami <andrea.adami@gmail.com>
Brad Bishop26bdd442019-08-16 17:08:17 -040023
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080024---
Patrick Williams520786c2023-06-25 16:20:36 -050025Upstream-Status: Pending
26
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080027 kexec/Makefile | 2 +-
Brad Bishop26bdd442019-08-16 17:08:17 -040028 kexec/if_nameindex.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
29 kexec/if_nameindex.h | 15 +++++++++++
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080030 kexec/ifdown.c | 3 +++
31 4 files changed, 83 insertions(+), 1 deletion(-)
32 create mode 100644 kexec/if_nameindex.c
33 create mode 100644 kexec/if_nameindex.h
34
35diff --git a/kexec/Makefile b/kexec/Makefile
36index 4db84d8..fb7520b 100644
37--- a/kexec/Makefile
38+++ b/kexec/Makefile
39@@ -11,7 +11,7 @@ KEXEC_SRCS = $(KEXEC_SRCS_base)
40 KEXEC_GENERATED_SRCS =
41
42 KEXEC_SRCS_base += kexec/kexec.c
43-KEXEC_SRCS_base += kexec/ifdown.c
44+KEXEC_SRCS_base += kexec/if_nameindex kexec/ifdown.c
45 KEXEC_SRCS_base += kexec/kexec-elf.c
46 KEXEC_SRCS_base += kexec/kexec-elf-exec.c
47 KEXEC_SRCS_base += kexec/kexec-elf-core.c
48diff --git a/kexec/if_nameindex.c b/kexec/if_nameindex.c
49new file mode 100644
50index 0000000..e586e41
51--- /dev/null
52+++ b/kexec/if_nameindex.c
53@@ -0,0 +1,64 @@
54+#define _GNU_SOURCE
55+#ifdef __KLIBC__
56+#include <sys/types.h>
57+#endif
58+#include <netinet/in.h>
59+#include <net/if.h>
60+#include <stdlib.h>
61+#include <sys/socket.h>
62+#include <sys/ioctl.h>
63+#include <errno.h>
64+#include <sys/syscall.h>
65+#include <stdio.h>
66+#ifdef __KLIBC__
67+#include "if_nameindex.h"
68+#endif
69+
70+static void *do_nameindex(int s, size_t n)
71+{
72+ size_t i, len, k;
73+ struct ifconf conf;
74+ struct if_nameindex *idx;
75+
76+ idx = malloc(n * (sizeof(struct if_nameindex)+sizeof(struct ifreq)));
77+ if (!idx) return 0;
78+
79+ conf.ifc_buf = (void *)&idx[n];
80+ conf.ifc_len = len = n * sizeof(struct ifreq);
81+ if (ioctl(s, SIOCGIFCONF, &conf) < 0) {
82+ free(idx);
83+ return 0;
84+ }
85+ if (conf.ifc_len == len) {
86+ free(idx);
87+ return (void *)-1;
88+ }
89+
90+ n = conf.ifc_len / sizeof(struct ifreq);
91+ for (i=k=0; i<n; i++) {
92+ if (ioctl(s, SIOCGIFINDEX, &conf.ifc_req[i]) < 0) {
93+ k++;
94+ continue;
95+ }
96+ idx[i-k].if_index = conf.ifc_req[i].ifr_ifindex;
97+ idx[i-k].if_name = conf.ifc_req[i].ifr_name;
98+ }
99+ idx[i-k].if_name = 0;
100+ idx[i-k].if_index = 0;
101+
102+ return idx;
103+}
104+
105+struct if_nameindex *if_nameindex()
106+{
107+ size_t n;
108+ void *p = 0;
109+ int s = socket(AF_UNIX, SOCK_DGRAM, 0);
110+ if (s>=0) {
111+ for (n=0; (p=do_nameindex(s, n)) == (void *)-1; n++);
112+/* __syscall(SYS_close, s); */
113+ close(s);
114+ }
115+ errno = ENOBUFS;
116+ return p;
117+}
118diff --git a/kexec/if_nameindex.h b/kexec/if_nameindex.h
119new file mode 100644
Brad Bishop26bdd442019-08-16 17:08:17 -0400120index 0000000..cf1c061
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800121--- /dev/null
122+++ b/kexec/if_nameindex.h
123@@ -0,0 +1,15 @@
124+#ifndef _NET_IF__NAMEINDEX_H
125+#define _NET_IF_NAMEINDEX_H
126+
127+struct if_nameindex
128+{
129+ unsigned int if_index;
130+ char *if_name;
131+};
132+
133+unsigned int if_nametoindex (const char *);
134+char *if_indextoname (unsigned int, char *);
135+struct if_nameindex *if_nameindex (void);
136+void if_freenameindex (struct if_nameindex *);
137+
138+#endif
139diff --git a/kexec/ifdown.c b/kexec/ifdown.c
140index 82c6141..cc3ca9f 100644
141--- a/kexec/ifdown.c
142+++ b/kexec/ifdown.c
143@@ -18,6 +18,9 @@ char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl";
144
145 #include <netinet/in.h>
146 #include <net/if.h>
147+#ifdef __KLIBC__
148+#include "if_nameindex.h"
149+#endif
150
151 /*
152 * First, we find all shaper devices and down them. Then we