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

« back to all changes in this revision

Viewing changes to fea/data_plane/ifconfig/ifconfig_vlan_get_bsd.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: 4; tab-width: 8; indent-tabs-mode: t -*-
 
2
 
 
3
// Copyright (c) 2007-2008 International Computer Science Institute
 
4
//
 
5
// Permission is hereby granted, free of charge, to any person obtaining a
 
6
// copy of this software and associated documentation files (the "Software")
 
7
// to deal in the Software without restriction, subject to the conditions
 
8
// listed in the XORP LICENSE file. These conditions include: you must
 
9
// preserve this copyright notice, and you cannot mention the copyright
 
10
// holders in advertising related to the Software without their permission.
 
11
// The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
 
12
// notice is a summary of the XORP LICENSE file; the license in that file is
 
13
// legally binding.
 
14
 
 
15
#ident "$XORP: xorp/fea/data_plane/ifconfig/ifconfig_vlan_get_bsd.cc,v 1.7 2008/01/04 03:16:10 pavlin Exp $"
 
16
 
 
17
#include "fea/fea_module.h"
 
18
 
 
19
#include "libxorp/xorp.h"
 
20
#include "libxorp/xlog.h"
 
21
#include "libxorp/debug.h"
 
22
#include "libxorp/ether_compat.h"
 
23
 
 
24
#include "libcomm/comm_api.h"
 
25
 
 
26
#ifdef HAVE_SYS_IOCTL_H
 
27
#include <sys/ioctl.h>
 
28
#endif
 
29
#ifdef HAVE_NET_IF_VLAN_VAR_H
 
30
#include <net/if_vlan_var.h>
 
31
#endif
 
32
#ifdef HAVE_NET_IF_VLANVAR_H
 
33
#include <net/if_vlanvar.h>
 
34
#endif
 
35
#ifdef HAVE_NET_VLAN_IF_VLAN_VAR_H
 
36
#include <net/vlan/if_vlan_var.h>
 
37
#endif
 
38
 
 
39
#include "fea/ifconfig.hh"
 
40
 
 
41
#include "ifconfig_vlan_get_bsd.hh"
 
42
 
 
43
 
 
44
//
 
45
// Get VLAN information about network interfaces from the underlying system.
 
46
//
 
47
// The mechanism to obtain the information is BSD-specific ioctl(2).
 
48
//
 
49
 
 
50
#ifdef HAVE_VLAN_BSD
 
51
 
 
52
IfConfigVlanGetBsd::IfConfigVlanGetBsd(FeaDataPlaneManager& fea_data_plane_manager)
 
53
    : IfConfigVlanGet(fea_data_plane_manager),
 
54
      _s4(-1)
 
55
{
 
56
}
 
57
 
 
58
IfConfigVlanGetBsd::~IfConfigVlanGetBsd()
 
59
{
 
60
    string error_msg;
 
61
 
 
62
    if (stop(error_msg) != XORP_OK) {
 
63
        XLOG_ERROR("Cannot stop the BSD-specific ioctl(2) mechanism to get "
 
64
                   "information about VLAN network interfaces from the "
 
65
                   "underlying system: %s",
 
66
                   error_msg.c_str());
 
67
    }
 
68
}
 
69
 
 
70
int
 
71
IfConfigVlanGetBsd::start(string& error_msg)
 
72
{
 
73
    if (_is_running)
 
74
        return (XORP_OK);
 
75
 
 
76
    if (_s4 < 0) {
 
77
        _s4 = socket(AF_INET, SOCK_DGRAM, 0);
 
78
        if (_s4 < 0) {
 
79
            error_msg = c_format("Could not initialize IPv4 ioctl() "
 
80
                                 "socket: %s", strerror(errno));
 
81
            XLOG_FATAL("%s", error_msg.c_str());
 
82
        }
 
83
    }
 
84
 
 
85
    _is_running = true;
 
86
 
 
87
    return (XORP_OK);
 
88
}
 
89
 
 
90
int
 
91
IfConfigVlanGetBsd::stop(string& error_msg)
 
92
{
 
93
    int ret_value4 = XORP_OK;
 
94
 
 
95
    if (! _is_running)
 
96
        return (XORP_OK);
 
97
 
 
98
    if (_s4 >= 0) {
 
99
        ret_value4 = comm_close(_s4);
 
100
        _s4 = -1;
 
101
        if (ret_value4 != XORP_OK) {
 
102
            error_msg = c_format("Could not close IPv4 ioctl() socket: %s",
 
103
                                 comm_get_last_error_str());
 
104
        }
 
105
    }
 
106
 
 
107
    if (ret_value4 != XORP_OK)
 
108
        return (XORP_ERROR);
 
109
 
 
110
    _is_running = false;
 
111
 
 
112
    return (XORP_OK);
 
113
}
 
114
 
 
115
int
 
116
IfConfigVlanGetBsd::pull_config(IfTree& iftree)
 
117
{
 
118
    return read_config(iftree);
 
119
}
 
120
 
 
121
int
 
122
IfConfigVlanGetBsd::read_config(IfTree& iftree)
 
123
{
 
124
    IfTree::IfMap::iterator ii;
 
125
    string error_msg;
 
126
 
 
127
    if (! _is_running) {
 
128
        error_msg = c_format("Cannot read VLAN interface intormation: "
 
129
                             "the IfConfigVlanGetBsd plugin is not running");
 
130
        XLOG_ERROR("%s", error_msg.c_str());
 
131
        return (XORP_ERROR);
 
132
    }
 
133
    XLOG_ASSERT(_s4 >= 0);
 
134
 
 
135
    //
 
136
    // Check all interfaces whether they are actually VLANs.
 
137
    // If an interface is found to be a VLAN, then its vif state is
 
138
    // copied as a vif child to its physical parent interface.
 
139
    // Note that we keep the original VLAN interface state in the IfTree
 
140
    // so the rest of the FEA is not surprised if that state suddenly is
 
141
    // deleted.
 
142
    //
 
143
    for (ii = iftree.interfaces().begin();
 
144
         ii != iftree.interfaces().end();
 
145
         ++ii) {
 
146
        IfTreeInterface* ifp = &ii->second;
 
147
        // Ignore interfaces that are to be deleted
 
148
        if (ifp->is_marked(IfTreeItem::DELETED))
 
149
            continue;
 
150
 
 
151
        struct ifreq ifreq;
 
152
        struct vlanreq vlanreq;
 
153
 
 
154
        // Test whether a VLAN interface
 
155
        memset(&ifreq, 0, sizeof(ifreq));
 
156
        memset(&vlanreq, 0, sizeof(vlanreq));
 
157
        strncpy(ifreq.ifr_name, ifp->ifname().c_str(),
 
158
                sizeof(ifreq.ifr_name) - 1);
 
159
        ifreq.ifr_data = reinterpret_cast<caddr_t>(&vlanreq);
 
160
        if (ioctl(_s4, SIOCGETVLAN, (caddr_t)&ifreq) < 0)
 
161
            continue;           // XXX: Most likely not a VLAN interface
 
162
 
 
163
        //
 
164
        // XXX: VLAN interface
 
165
        //
 
166
 
 
167
        // Get the VLAN information
 
168
        uint16_t vlan_id = vlanreq.vlr_tag;
 
169
        string parent_ifname = vlanreq.vlr_parent;
 
170
 
 
171
        if (parent_ifname.empty())
 
172
            continue;
 
173
 
 
174
        IfTreeInterface* parent_ifp = iftree.find_interface(parent_ifname);
 
175
        if ((parent_ifp == NULL) || parent_ifp->is_marked(IfTreeItem::DELETED))
 
176
            continue;
 
177
 
 
178
        // Find or add the VLAN vif
 
179
        IfTreeVif* parent_vifp = parent_ifp->find_vif(ifp->ifname());
 
180
        if (parent_vifp == NULL) {
 
181
            parent_ifp->add_vif(ifp->ifname());
 
182
            parent_vifp = parent_ifp->find_vif(ifp->ifname());
 
183
        }
 
184
        XLOG_ASSERT(parent_vifp != NULL);
 
185
 
 
186
        // Copy the vif state
 
187
        IfTreeVif* vifp = ifp->find_vif(ifp->ifname());
 
188
        if (vifp != NULL) {
 
189
            parent_vifp->copy_state(*vifp);
 
190
            parent_vifp->ipv4addrs() = vifp->ipv4addrs();
 
191
            parent_vifp->ipv6addrs() = vifp->ipv6addrs();
 
192
        }
 
193
 
 
194
        // Set the VLAN vif info
 
195
        parent_vifp->set_vlan(true);
 
196
        parent_vifp->set_vlan_id(vlan_id);
 
197
    }
 
198
 
 
199
    return (XORP_OK);
 
200
}
 
201
 
 
202
#endif // HAVE_VLAN_BSD