~ubuntu-branches/ubuntu/jaunty/xorp/jaunty

« back to all changes in this revision

Viewing changes to fea/pa_backend_nf.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jose Calhariz, Javier Fernandez-Sanguino, Jose Calhariz
  • Date: 2008-01-23 01:24:37 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080123012437-7l2u9r0k8e7op8st
Tags: 1.5~cvs.20080128-1
[ Javier Fernandez-Sanguino ]
* Update to latest CVS contents
* Modify debian/rules to prevent autobuilders from building 
  the binary-independent components: (Closes: #441121)
  - Create a new Build-Depends-Indep with all the TeX
  components used to build documentation
  - Since autobuilders call build, which in turns calls build-indep, hack
    the debian rules file so that the documentation is only built if ps2pdf,
    dvips and pslatex are available. 
* Modify the init.d script:
  - restart action: Do not attempt to stop xorp if not running
  - stop function: fix errors in the script
  - add a try-restart action
  - restructure the init.d script, move the restart code to a function
  - review the use of echo calls and exit values
* Use, as examples, the new boot files at rtrmgr/config/

[ Jose Calhariz ]
* Add depends on ncurses-dev, I don't know why xorp use tigetstr
  function from curses.  This way the depends field change less between
  build environments.
* Removed pushd and popd commands from Makefile and replaced with cd
  commands, was a bashism and FTBFS (closes: #453637)
* debian/control converted to utf-8 (closes: #454026) (closes: #453485)
* init.d/xorp now returns 0 if disabled.
* Added Vcs-Browser and Vcs-Svn fields pointing to the repository of the
  package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- c-basic-offset: 6; tab-width: 8; indent-tabs-mode: t -*-
 
1
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
2
2
// vim:set sts=4 ts=8:
3
3
 
4
 
// Copyright (c) 2001-2007 International Computer Science Institute
 
4
// Copyright (c) 2001-2008 International Computer Science Institute
5
5
//
6
6
// Permission is hereby granted, free of charge, to any person obtaining a
7
7
// copy of this software and associated documentation files (the "Software")
13
13
// notice is a summary of the XORP LICENSE file; the license in that file is
14
14
// legally binding.
15
15
 
16
 
#ident "$XORP: xorp/fea/pa_backend_nf.cc,v 1.2 2007/02/16 22:45:48 pavlin Exp $"
 
16
#ident "$XORP: xorp/fea/pa_backend_nf.cc,v 1.5 2008/01/04 03:15:50 pavlin Exp $"
17
17
 
18
18
#include "fea_module.h"
19
19
 
65
65
/* ------------------------------------------------------------------------- */
66
66
/* Constructor and destructor. */
67
67
 
68
 
PaNfBackend::PaNfBackend()
69
 
    throw(PaInvalidBackendException)
 
68
PaNfBackend::PaNfBackend() throw(PaInvalidBackendException)
70
69
    : _fd4(-1),
71
70
      _fd6(-1)
72
71
{
125
124
/* ------------------------------------------------------------------------- */
126
125
/* IPv4 ACL back-end methods. */
127
126
 
128
 
bool
 
127
int
129
128
PaNfBackend::push_entries4(const PaSnapshot4* snap)
130
129
{
131
130
#ifndef HAVE_PACKETFILTER_NF
132
131
    UNUSED(snap);
133
 
    return (false);
 
132
    return (XORP_ERROR);
134
133
#else
135
134
    XLOG_ASSERT(snap != NULL);
136
135
 
137
136
    const PaSnapTable4& table = snap->data();
138
 
    bool result;
 
137
    int ret_value;
139
138
    struct ipt_entry *ipt;
140
139
    char *buf;
141
140
    ssize_t size;
148
147
    ipt = malloc(tsize);
149
148
    if (ipt == NULL) {
150
149
        XLOG_ERROR("malloc: %s", strerror(errno));
151
 
        return (false);
 
150
        return (XORP_ERROR);
152
151
    }
153
152
#endif
154
153
 
155
 
    result = false;
 
154
    ret_value = XORP_ERROR;
156
155
 
157
156
    // Transcribe all the rules contained within the snapshot
158
157
    // into Netfilter format.
159
158
    pipt = ipt;
160
159
    for (PaSnapTable4::const_iterator i = table.begin();
161
160
         i != table.end(); i++) {
162
 
        result = transcribe_rule4(*i, pipt);
163
 
        if (!result)
 
161
        ret_value = transcribe_rule4(*i, pipt);
 
162
        if (ret_value != XORP_OK)
164
163
            break;
165
164
        ++pipt;
166
165
    }
167
166
 
168
167
    // Push the new ruleset to Netfilter.
169
 
    result = push_rules4(tsize, table.size(), ipt);
 
168
    ret_value = push_rules4(tsize, table.size(), ipt);
170
169
 
171
 
    return (result);
 
170
    return (ret_value);
172
171
#endif
173
172
}
174
173
 
175
174
// Clear all entries by committing an empty transaction.
176
 
bool
 
175
int
177
176
PaNfBackend::delete_all_entries4()
178
177
{
179
178
#ifndef HAVE_PACKETFILTER_NF
180
 
    return (false);
 
179
    return (XORP_ERROR);
181
180
#else
182
181
    return (push_rules4(0, 0, NULL));
183
182
#endif
218
217
    ige = malloc(bufsize);
219
218
    if (ige == NULL) {
220
219
        XLOG_ERROR("Cannot allocate NF rule state: malloc: %s",
221
 
                    strerror(errno));
 
220
                   strerror(errno));
222
221
        return (NULL);
223
222
    }
224
223
 
226
225
    // Request the firewall rule list from the kernel.
227
226
    //
228
227
    strncpy(ige->name, "FILTER", IPT_TABLE_MAXNAMELEN);
229
 
    if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, ige, &bufsize) < 0)
 
228
    if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, ige, &bufsize) < 0) {
230
229
        XLOG_ERROR("Cannot snapshot NF rule state: SO_GET_ENTRIES: %s",
231
230
                   strerror(errno));
 
231
        return (NULL);
232
232
    }
233
233
 
234
234
    // Create a snapshot of the current NF rule set using the
243
243
#endif
244
244
}
245
245
 
246
 
bool
 
246
int
247
247
PaNfBackend::restore_snapshot4(const PaBackend::Snapshot4Base* snap4)
248
248
{
249
249
#ifndef HAVE_PACKETFILTER_NF
250
250
    UNUSED(snap4);
251
 
    return (false);
 
251
    return (XORP_ERROR);
252
252
#else
253
253
    // Simply check if the snapshot is an instance of our derived snapshot.
254
254
    // If it is not, we received it in error, and it is of no interest to us.
269
269
 
270
270
// Push a ruleset to the kernel, specified as a buffer containing
271
271
// a vector of variable-sized ipt_entry elements.
272
 
bool
 
272
int
273
273
PaNfBackend::push_rules4(unsigned int size, unsigned int num_entries,
274
274
                         struct ipt_entry *rules)
275
275
{
276
276
    struct ipt_replace *ipr;
277
277
    unsigned int rsize;
278
 
    bool result;
 
278
    int ret_value;
279
279
    //
280
280
    // Allocate buffer for the ipt_replace message, fill
281
281
    // it out, and copy the rule list vector into the message.
289
289
    ipr = malloc(rsize);
290
290
    if (ipr == NULL) {
291
291
        XLOG_ERROR("malloc: %s", strerror(errno));
292
 
        return (false);
 
292
        return (XORP_ERROR);
293
293
    }
294
294
    memset(ipr, 0, sizeof(ipr));
295
295
    strncpy(ipr->name, "FILTER", IPT_TABLE_MAXNAMELEN);
296
 
    if (rules)
 
296
    if (rules) {
297
297
        memcpy(ipr->entries, rules, size);
298
298
        ipr->num_entries = num_entries;
299
299
        ipr->size = size;
301
301
        ipr->hook_entry[NF_IP_FORWARD] = 0;
302
302
    }
303
303
 
304
 
    result = true;
 
304
    ret_value = XORP_OK;
305
305
 
306
306
    if (setsockopt(_fd4, IPPROTO_IP, IPT_SO_SET_REPLACE, ipr, rsize) < 0) {
307
307
        XLOG_ERROR("Failed to commit transaction for adding rules"
308
308
                   "to NF firewall: setsockopt SO_SET_REPLACE: %s",
309
 
                    strerror(errno));
310
 
        result = false;
 
309
                   strerror(errno));
 
310
        ret_value = XORP_ERROR;
311
311
    }
312
312
 
313
313
    free(ipr);
314
 
    return (result);
 
314
    return (ret_value);
315
315
}
316
316
 
317
317
// Return 0 on failure, otherwise, size of rule required or transcribed.
324
324
    // it out. We can use this as a sanity check.
325
325
    //
326
326
    ssize_t needed = sizeof(struct ipt_entry) +
327
 
                     sizeof(struct ipt_standard_target);
 
327
        sizeof(struct ipt_standard_target);
328
328
    if (ipt->ip_proto == IPPROTO_TCP || ipt->ip_proto == IPPROTO_UDP)
329
329
        needed += sizeof(struct ipt_entry_match); // XXX
330
330
    //