source: branches/stable/mindi-busybox/networking/libiproute/iprule.c

Last change on this file was 1770, checked in by Bruno Cornec, 13 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod? silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian? keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René? Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * iprule.c     "ip rule".
4 *
5 *      This program is free software; you can redistribute it and/or
6 *      modify it under the terms of the GNU General Public License
7 *      as published by the Free Software Foundation; either version
8 *      2 of the License, or (at your option) any later version.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 *
12 *
13 * Changes:
14 *
15 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
16 * initially integrated into busybox by Bernhard Fischer
17 */
18
19#include <syslog.h>
20//#include <sys/socket.h>
21#include <netinet/in.h>
22#include <netinet/ip.h>
23#include <arpa/inet.h>
24
25#include "ip_common.h"  /* #include "libbb.h" is inside */
26#include "rt_names.h"
27#include "utils.h"
28
29/*
30static void usage(void) __attribute__((noreturn));
31
32static void usage(void)
33{
34    fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
35    fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
36    fprintf(stderr, "            [ dev STRING ] [ pref NUMBER ]\n");
37    fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
38    fprintf(stderr, "          [ prohibit | reject | unreachable ]\n");
39    fprintf(stderr, "          [ realms [SRCREALM/]DSTREALM ]\n");
40    fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
41    exit(-1);
42}
43*/
44
45static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
46                    struct nlmsghdr *n, void *arg)
47{
48    FILE *fp = (FILE*)arg;
49    struct rtmsg *r = NLMSG_DATA(n);
50    int len = n->nlmsg_len;
51    int host_len = -1;
52    struct rtattr * tb[RTA_MAX+1];
53    char abuf[256];
54    SPRINT_BUF(b1);
55
56    if (n->nlmsg_type != RTM_NEWRULE)
57        return 0;
58
59    len -= NLMSG_LENGTH(sizeof(*r));
60    if (len < 0)
61        return -1;
62
63    memset(tb, 0, sizeof(tb));
64    parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
65
66    if (r->rtm_family == AF_INET)
67        host_len = 32;
68    else if (r->rtm_family == AF_INET6)
69        host_len = 128;
70/*  else if (r->rtm_family == AF_DECnet)
71        host_len = 16;
72    else if (r->rtm_family == AF_IPX)
73        host_len = 80;
74*/
75    if (tb[RTA_PRIORITY])
76        fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
77    else
78        fprintf(fp, "0:\t");
79
80    fprintf(fp, "from ");
81    if (tb[RTA_SRC]) {
82        if (r->rtm_src_len != host_len) {
83            fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family,
84                             RTA_PAYLOAD(tb[RTA_SRC]),
85                             RTA_DATA(tb[RTA_SRC]),
86                             abuf, sizeof(abuf)),
87                r->rtm_src_len
88                );
89        } else {
90            fprintf(fp, "%s", format_host(r->rtm_family,
91                               RTA_PAYLOAD(tb[RTA_SRC]),
92                               RTA_DATA(tb[RTA_SRC]),
93                               abuf, sizeof(abuf))
94                );
95        }
96    } else if (r->rtm_src_len) {
97        fprintf(fp, "0/%d", r->rtm_src_len);
98    } else {
99        fprintf(fp, "all");
100    }
101    fprintf(fp, " ");
102
103    if (tb[RTA_DST]) {
104        if (r->rtm_dst_len != host_len) {
105            fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
106                             RTA_PAYLOAD(tb[RTA_DST]),
107                             RTA_DATA(tb[RTA_DST]),
108                             abuf, sizeof(abuf)),
109                r->rtm_dst_len
110                );
111        } else {
112            fprintf(fp, "to %s ", format_host(r->rtm_family,
113                               RTA_PAYLOAD(tb[RTA_DST]),
114                               RTA_DATA(tb[RTA_DST]),
115                               abuf, sizeof(abuf)));
116        }
117    } else if (r->rtm_dst_len) {
118        fprintf(fp, "to 0/%d ", r->rtm_dst_len);
119    }
120
121    if (r->rtm_tos) {
122        fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
123    }
124    if (tb[RTA_PROTOINFO]) {
125        fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
126    }
127
128    if (tb[RTA_IIF]) {
129        fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
130    }
131
132    if (r->rtm_table)
133        fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
134
135    if (tb[RTA_FLOW]) {
136        uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
137        uint32_t from = to>>16;
138        to &= 0xFFFF;
139        if (from) {
140            fprintf(fp, "realms %s/",
141                rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
142        }
143        fprintf(fp, "%s ",
144            rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
145    }
146
147    if (r->rtm_type == RTN_NAT) {
148        if (tb[RTA_GATEWAY]) {
149            fprintf(fp, "map-to %s ",
150                format_host(r->rtm_family,
151                        RTA_PAYLOAD(tb[RTA_GATEWAY]),
152                        RTA_DATA(tb[RTA_GATEWAY]),
153                        abuf, sizeof(abuf)));
154        } else
155            fprintf(fp, "masquerade");
156    } else if (r->rtm_type != RTN_UNICAST)
157        fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
158
159    fputc('\n', fp);
160    fflush(fp);
161    return 0;
162}
163
164/* Return value becomes exitcode. It's okay to not return at all */
165static int iprule_list(int argc, char **argv)
166{
167    struct rtnl_handle rth;
168    int af = preferred_family;
169
170    if (af == AF_UNSPEC)
171        af = AF_INET;
172
173    if (argc > 0) {
174        //bb_error_msg("\"rule show\" needs no arguments");
175        bb_warn_ignoring_args(argc);
176        return -1;
177    }
178
179    xrtnl_open(&rth);
180
181    xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
182    xrtnl_dump_filter(&rth, print_rule, stdout);
183
184    return 0;
185}
186
187/* Return value becomes exitcode. It's okay to not return at all */
188static int iprule_modify(int cmd, int argc, char **argv)
189{
190    static const char keywords[] ALIGN1 =
191        "from\0""to\0""preference\0""order\0""priority\0"
192        "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
193        "iif\0""nat\0""map-to\0""type\0""help\0";
194    enum {
195        ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
196        ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
197        ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
198    };
199    bool table_ok = 0;
200    struct rtnl_handle rth;
201    struct {
202        struct nlmsghdr n;
203        struct rtmsg    r;
204        char        buf[1024];
205    } req;
206    smalluint key;
207
208    memset(&req, 0, sizeof(req));
209
210    req.n.nlmsg_type = cmd;
211    req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
212    req.n.nlmsg_flags = NLM_F_REQUEST;
213    req.r.rtm_family = preferred_family;
214    req.r.rtm_protocol = RTPROT_BOOT;
215    req.r.rtm_scope = RT_SCOPE_UNIVERSE;
216    req.r.rtm_table = 0;
217    req.r.rtm_type = RTN_UNSPEC;
218
219    if (cmd == RTM_NEWRULE) {
220        req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
221        req.r.rtm_type = RTN_UNICAST;
222    }
223
224    while (argc > 0) {
225        key = index_in_substrings(keywords, *argv) + 1;
226        if (key == 0) /* no match found in keywords array, bail out. */
227            bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
228        if (key == ARG_from) {
229            inet_prefix dst;
230            NEXT_ARG();
231            get_prefix(&dst, *argv, req.r.rtm_family);
232            req.r.rtm_src_len = dst.bitlen;
233            addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
234        } else if (key == ARG_to) {
235            inet_prefix dst;
236            NEXT_ARG();
237            get_prefix(&dst, *argv, req.r.rtm_family);
238            req.r.rtm_dst_len = dst.bitlen;
239            addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
240        } else if (key == ARG_preference ||
241               key == ARG_order ||
242               key == ARG_priority) {
243            uint32_t pref;
244            NEXT_ARG();
245            if (get_u32(&pref, *argv, 0))
246                invarg(*argv, "preference");
247            addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
248        } else if (key == ARG_tos) {
249            uint32_t tos;
250            NEXT_ARG();
251            if (rtnl_dsfield_a2n(&tos, *argv))
252                invarg(*argv, "TOS");
253            req.r.rtm_tos = tos;
254        } else if (key == ARG_fwmark) {
255            uint32_t fwmark;
256            NEXT_ARG();
257            if (get_u32(&fwmark, *argv, 0))
258                invarg(*argv, "fwmark");
259            addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
260        } else if (key == ARG_realms) {
261            uint32_t realm;
262            NEXT_ARG();
263            if (get_rt_realms(&realm, *argv))
264                invarg(*argv, "realms");
265            addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
266        } else if (key == ARG_table ||
267               key == ARG_lookup) {
268            uint32_t tid;
269            NEXT_ARG();
270            if (rtnl_rttable_a2n(&tid, *argv))
271                invarg(*argv, "table ID");
272            req.r.rtm_table = tid;
273            table_ok = 1;
274        } else if (key == ARG_dev ||
275               key == ARG_iif) {
276            NEXT_ARG();
277            addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
278        } else if (key == ARG_nat ||
279               key == ARG_map_to) {
280            NEXT_ARG();
281            addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
282            req.r.rtm_type = RTN_NAT;
283        } else {
284            int type;
285
286            if (key == ARG_type) {
287                NEXT_ARG();
288            }
289            if (key == ARG_help)
290                bb_show_usage();
291            if (rtnl_rtntype_a2n(&type, *argv))
292                invarg(*argv, "type");
293            req.r.rtm_type = type;
294        }
295        argc--;
296        argv++;
297    }
298
299    if (req.r.rtm_family == AF_UNSPEC)
300        req.r.rtm_family = AF_INET;
301
302    if (!table_ok && cmd == RTM_NEWRULE)
303        req.r.rtm_table = RT_TABLE_MAIN;
304
305    xrtnl_open(&rth);
306
307    if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
308        return 2;
309
310    return 0;
311}
312
313/* Return value becomes exitcode. It's okay to not return at all */
314int do_iprule(int argc, char **argv)
315{
316    static const char ip_rule_commands[] ALIGN1 =
317        "add\0""delete\0""list\0""show\0";
318    int cmd = 2; /* list */
319
320    if (argc < 1)
321        return iprule_list(0, NULL);
322    if (*argv)
323        cmd = index_in_substrings(ip_rule_commands, *argv);
324
325    switch (cmd) {
326        case 0: /* add */
327            cmd = RTM_NEWRULE;
328            break;
329        case 1: /* delete */
330            cmd = RTM_DELRULE;
331            break;
332        case 2: /* list */
333        case 3: /* show */
334            return iprule_list(argc-1, argv+1);
335            break;
336        default:
337            bb_error_msg_and_die("unknown command %s", *argv);
338    }
339    return iprule_modify(cmd, argc-1, argv+1);
340}
Note: See TracBrowser for help on using the repository browser.