~ubuntu-branches/ubuntu/raring/ipxe/raring

« back to all changes in this revision

Viewing changes to src/net/arp.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-11-14 15:47:31 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20121114154731-jhuy5d1h2jw75qe9
Tags: 1.0.0+git-4.d6b0b76-0ubuntu1
* New upstream snapshot:
  - d/p/iscsi*.patch: Dropped - included in snapshot.
  - Refreshed all other patches.
* d/p/enable-https.patch: Enable HTTPS support (LP: #1025239).

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 *
14
14
 * You should have received a copy of the GNU General Public License
15
15
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301, USA.
17
18
 */
18
19
 
19
20
FILE_LICENCE ( GPL2_OR_LATER );
20
21
 
21
22
#include <stdint.h>
 
23
#include <stdlib.h>
22
24
#include <string.h>
23
25
#include <byteswap.h>
24
26
#include <errno.h>
26
28
#include <ipxe/if_arp.h>
27
29
#include <ipxe/iobuf.h>
28
30
#include <ipxe/netdevice.h>
 
31
#include <ipxe/list.h>
 
32
#include <ipxe/retry.h>
 
33
#include <ipxe/timer.h>
 
34
#include <ipxe/malloc.h>
 
35
#include <ipxe/refcnt.h>
29
36
#include <ipxe/arp.h>
30
37
 
31
38
/** @file
38
45
 *
39
46
 */
40
47
 
 
48
/** ARP minimum timeout */
 
49
#define ARP_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
 
50
 
 
51
/** ARP maximum timeout */
 
52
#define ARP_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
 
53
 
41
54
/** An ARP cache entry */
42
55
struct arp_entry {
 
56
        /** Reference count */
 
57
        struct refcnt refcnt;
 
58
        /** List of ARP cache entries */
 
59
        struct list_head list;
 
60
        /** Network device */
 
61
        struct net_device *netdev;
43
62
        /** Network-layer protocol */
44
63
        struct net_protocol *net_protocol;
45
 
        /** Link-layer protocol */
46
 
        struct ll_protocol *ll_protocol;
47
 
        /** Network-layer address */
48
 
        uint8_t net_addr[MAX_NET_ADDR_LEN];
49
 
        /** Link-layer address */
50
 
        uint8_t ll_addr[MAX_LL_ADDR_LEN];
 
64
        /** Network-layer destination address */
 
65
        uint8_t net_dest[MAX_NET_ADDR_LEN];
 
66
        /** Network-layer source address */
 
67
        uint8_t net_source[MAX_NET_ADDR_LEN];
 
68
        /** Link-layer destination address */
 
69
        uint8_t ll_dest[MAX_LL_ADDR_LEN];
 
70
        /** Retransmission timer */
 
71
        struct retry_timer timer;
 
72
        /** Pending I/O buffers */
 
73
        struct list_head tx_queue;
51
74
};
52
75
 
53
 
/** Number of entries in the ARP cache
54
 
 *
55
 
 * This is a global cache, covering all network interfaces,
56
 
 * network-layer protocols and link-layer protocols.
57
 
 */
58
 
#define NUM_ARP_ENTRIES 4
59
 
 
60
76
/** The ARP cache */
61
 
static struct arp_entry arp_table[NUM_ARP_ENTRIES];
62
 
#define arp_table_end &arp_table[NUM_ARP_ENTRIES]
63
 
 
64
 
static unsigned int next_new_arp_entry = 0;
 
77
static LIST_HEAD ( arp_entries );
65
78
 
66
79
struct net_protocol arp_protocol __net_protocol;
67
80
 
 
81
static void arp_expired ( struct retry_timer *timer, int over );
 
82
 
 
83
/**
 
84
 * Free ARP cache entry
 
85
 *
 
86
 * @v refcnt            Reference count
 
87
 */
 
88
static void arp_free ( struct refcnt *refcnt ) {
 
89
        struct arp_entry *arp =
 
90
                container_of ( refcnt, struct arp_entry, refcnt );
 
91
 
 
92
        /* Sanity check */
 
93
        assert ( list_empty ( &arp->tx_queue ) );
 
94
 
 
95
        /* Drop reference to network device */
 
96
        netdev_put ( arp->netdev );
 
97
 
 
98
        /* Free entry */
 
99
        free ( arp );
 
100
}
 
101
 
 
102
/**
 
103
 * Create ARP cache entry
 
104
 *
 
105
 * @v netdev            Network device
 
106
 * @v net_protocol      Network-layer protocol
 
107
 * @v net_dest          Destination network-layer address
 
108
 * @v net_source        Source network-layer address
 
109
 * @ret arp             ARP cache entry, or NULL if allocation failed
 
110
 */
 
111
static struct arp_entry * arp_create ( struct net_device *netdev,
 
112
                                       struct net_protocol *net_protocol,
 
113
                                       const void *net_dest,
 
114
                                       const void *net_source ) {
 
115
        struct arp_entry *arp;
 
116
 
 
117
        /* Allocate and initialise entry */
 
118
        arp = zalloc ( sizeof ( *arp ) );
 
119
        if ( ! arp )
 
120
                return NULL;
 
121
        ref_init ( &arp->refcnt, arp_free );
 
122
        arp->netdev = netdev_get ( netdev );
 
123
        arp->net_protocol = net_protocol;
 
124
        memcpy ( arp->net_dest, net_dest,
 
125
                 net_protocol->net_addr_len );
 
126
        memcpy ( arp->net_source, net_source,
 
127
                 net_protocol->net_addr_len );
 
128
        timer_init ( &arp->timer, arp_expired, &arp->refcnt );
 
129
        arp->timer.min_timeout = ARP_MIN_TIMEOUT;
 
130
        arp->timer.max_timeout = ARP_MAX_TIMEOUT;
 
131
        INIT_LIST_HEAD ( &arp->tx_queue );
 
132
 
 
133
        /* Start timer running to trigger initial transmission */
 
134
        start_timer_nodelay ( &arp->timer );
 
135
 
 
136
        /* Transfer ownership to cache */
 
137
        list_add ( &arp->list, &arp_entries );
 
138
 
 
139
        DBGC ( arp, "ARP %p %s %s %s created\n", arp, netdev->name,
 
140
               net_protocol->name, net_protocol->ntoa ( net_dest ) );
 
141
        return arp;
 
142
}
 
143
 
68
144
/**
69
145
 * Find entry in the ARP cache
70
146
 *
71
 
 * @v ll_protocol       Link-layer protocol
 
147
 * @v netdev            Network device
72
148
 * @v net_protocol      Network-layer protocol
73
 
 * @v net_addr          Network-layer address
 
149
 * @v net_dest          Destination network-layer address
74
150
 * @ret arp             ARP cache entry, or NULL if not found
75
 
 *
76
151
 */
77
 
static struct arp_entry *
78
 
arp_find_entry ( struct ll_protocol *ll_protocol,
79
 
                 struct net_protocol *net_protocol,
80
 
                 const void *net_addr ) {
 
152
static struct arp_entry * arp_find ( struct net_device *netdev,
 
153
                                     struct net_protocol *net_protocol,
 
154
                                     const void *net_dest ) {
81
155
        struct arp_entry *arp;
82
156
 
83
 
        for ( arp = arp_table ; arp < arp_table_end ; arp++ ) {
84
 
                if ( ( arp->ll_protocol == ll_protocol ) &&
 
157
        list_for_each_entry ( arp, &arp_entries, list ) {
 
158
                if ( ( arp->netdev == netdev ) &&
85
159
                     ( arp->net_protocol == net_protocol ) &&
86
 
                     ( memcmp ( arp->net_addr, net_addr,
87
 
                                net_protocol->net_addr_len ) == 0 ) )
 
160
                     ( memcmp ( arp->net_dest, net_dest,
 
161
                                net_protocol->net_addr_len ) == 0 ) ) {
 
162
 
 
163
                        /* Move to start of cache */
 
164
                        list_del ( &arp->list );
 
165
                        list_add ( &arp->list, &arp_entries );
 
166
 
88
167
                        return arp;
 
168
                }
89
169
        }
90
170
        return NULL;
91
171
}
92
172
 
93
173
/**
94
 
 * Look up media-specific link-layer address in the ARP cache
95
 
 *
 
174
 * Destroy ARP cache entry
 
175
 *
 
176
 * @v arp               ARP cache entry
 
177
 * @v rc                Reason for destruction
 
178
 */
 
179
static void arp_destroy ( struct arp_entry *arp, int rc ) {
 
180
        struct net_device *netdev = arp->netdev;
 
181
        struct net_protocol *net_protocol = arp->net_protocol;
 
182
        struct io_buffer *iobuf;
 
183
 
 
184
        /* Take ownership from cache */
 
185
        list_del ( &arp->list );
 
186
 
 
187
        /* Stop timer */
 
188
        stop_timer ( &arp->timer );
 
189
 
 
190
        /* Discard any outstanding I/O buffers */
 
191
        while ( ( iobuf = list_first_entry ( &arp->tx_queue, struct io_buffer,
 
192
                                             list ) ) != NULL ) {
 
193
                DBGC2 ( arp, "ARP %p %s %s %s discarding deferred packet: "
 
194
                        "%s\n", arp, netdev->name, net_protocol->name,
 
195
                        net_protocol->ntoa ( arp->net_dest ), strerror ( rc ) );
 
196
                list_del ( &iobuf->list );
 
197
                netdev_tx_err ( arp->netdev, iobuf, rc );
 
198
        }
 
199
 
 
200
        DBGC ( arp, "ARP %p %s %s %s destroyed: %s\n", arp, netdev->name,
 
201
               net_protocol->name, net_protocol->ntoa ( arp->net_dest ),
 
202
               strerror ( rc ) );
 
203
 
 
204
        /* Drop remaining reference */
 
205
        ref_put ( &arp->refcnt );
 
206
}
 
207
 
 
208
/**
 
209
 * Test if ARP cache entry has a valid link-layer address
 
210
 *
 
211
 * @v arp               ARP cache entry
 
212
 * @ret resolved        ARP cache entry is resolved
 
213
 */
 
214
static inline int arp_resolved ( struct arp_entry *arp ) {
 
215
        return ( ! timer_running ( &arp->timer ) );
 
216
}
 
217
 
 
218
/**
 
219
 * Transmit packet, determining link-layer address via ARP
 
220
 *
 
221
 * @v iobuf             I/O buffer
96
222
 * @v netdev            Network device
97
223
 * @v net_protocol      Network-layer protocol
98
 
 * @v dest_net_addr     Destination network-layer address
99
 
 * @v source_net_addr   Source network-layer address
100
 
 * @ret dest_ll_addr    Destination link layer address
 
224
 * @v net_dest          Destination network-layer address
 
225
 * @v net_source        Source network-layer address
 
226
 * @v ll_source         Source link-layer address
101
227
 * @ret rc              Return status code
102
 
 *
103
 
 * This function will use the ARP cache to look up the link-layer
104
 
 * address for the link-layer protocol associated with the network
105
 
 * device and the given network-layer protocol and addresses.  If
106
 
 * found, the destination link-layer address will be filled in in @c
107
 
 * dest_ll_addr.
108
 
 *
109
 
 * If no address is found in the ARP cache, an ARP request will be
110
 
 * transmitted on the specified network device and -ENOENT will be
111
 
 * returned.
112
 
 */
113
 
int arp_resolve ( struct net_device *netdev, struct net_protocol *net_protocol,
114
 
                  const void *dest_net_addr, const void *source_net_addr,
115
 
                  void *dest_ll_addr ) {
116
 
        struct ll_protocol *ll_protocol = netdev->ll_protocol;
117
 
        const struct arp_entry *arp;
 
228
 */
 
229
int arp_tx ( struct io_buffer *iobuf, struct net_device *netdev,
 
230
             struct net_protocol *net_protocol, const void *net_dest,
 
231
             const void *net_source, const void *ll_source ) {
 
232
        struct arp_entry *arp;
 
233
 
 
234
        /* Find or create ARP cache entry */
 
235
        arp = arp_find ( netdev, net_protocol, net_dest );
 
236
        if ( ! arp ) {
 
237
                arp = arp_create ( netdev, net_protocol, net_dest,
 
238
                                   net_source );
 
239
                if ( ! arp )
 
240
                        return -ENOMEM;
 
241
        }
 
242
 
 
243
        /* If a link-layer address is available then transmit
 
244
         * immediately, otherwise queue for later transmission.
 
245
         */
 
246
        if ( arp_resolved ( arp ) ) {
 
247
                return net_tx ( iobuf, netdev, net_protocol, arp->ll_dest,
 
248
                                ll_source );
 
249
        } else {
 
250
                DBGC2 ( arp, "ARP %p %s %s %s deferring packet\n",
 
251
                        arp, netdev->name, net_protocol->name,
 
252
                        net_protocol->ntoa ( net_dest ) );
 
253
                list_add_tail ( &iobuf->list, &arp->tx_queue );
 
254
                return -EAGAIN;
 
255
        }
 
256
}
 
257
 
 
258
/**
 
259
 * Update ARP cache entry
 
260
 *
 
261
 * @v arp               ARP cache entry
 
262
 * @v ll_dest           Destination link-layer address
 
263
 */
 
264
static void arp_update ( struct arp_entry *arp, const void *ll_dest ) {
 
265
        struct net_device *netdev = arp->netdev;
 
266
        struct ll_protocol *ll_protocol = netdev->ll_protocol;
 
267
        struct net_protocol *net_protocol = arp->net_protocol;
 
268
        struct io_buffer *iobuf;
 
269
        int rc;
 
270
 
 
271
        DBGC ( arp, "ARP %p %s %s %s updated => %s\n", arp, netdev->name,
 
272
               net_protocol->name, net_protocol->ntoa ( arp->net_dest ),
 
273
               ll_protocol->ntoa ( ll_dest ) );
 
274
 
 
275
        /* Fill in link-layer address */
 
276
        memcpy ( arp->ll_dest, ll_dest, ll_protocol->ll_addr_len );
 
277
 
 
278
        /* Stop retransmission timer */
 
279
        stop_timer ( &arp->timer );
 
280
 
 
281
        /* Transmit any packets in queue.  Take out a temporary
 
282
         * reference on the entry to prevent it from going out of
 
283
         * scope during the call to net_tx().
 
284
         */
 
285
        ref_get ( &arp->refcnt );
 
286
        while ( ( iobuf = list_first_entry ( &arp->tx_queue, struct io_buffer,
 
287
                                             list ) ) != NULL ) {
 
288
                DBGC2 ( arp, "ARP %p %s %s %s transmitting deferred packet\n",
 
289
                        arp, netdev->name, net_protocol->name,
 
290
                        net_protocol->ntoa ( arp->net_dest ) );
 
291
                list_del ( &iobuf->list );
 
292
                if ( ( rc = net_tx ( iobuf, netdev, net_protocol, ll_dest,
 
293
                                     netdev->ll_addr ) ) != 0 ) {
 
294
                        DBGC ( arp, "ARP %p could not transmit deferred "
 
295
                               "packet: %s\n", arp, strerror ( rc ) );
 
296
                        /* Ignore error and continue */
 
297
                }
 
298
        }
 
299
        ref_put ( &arp->refcnt );
 
300
}
 
301
 
 
302
/**
 
303
 * Handle ARP timer expiry
 
304
 *
 
305
 * @v timer             Retry timer
 
306
 * @v fail              Failure indicator
 
307
 */
 
308
static void arp_expired ( struct retry_timer *timer, int fail ) {
 
309
        struct arp_entry *arp = container_of ( timer, struct arp_entry, timer );
 
310
        struct net_device *netdev = arp->netdev;
 
311
        struct ll_protocol *ll_protocol = netdev->ll_protocol;
 
312
        struct net_protocol *net_protocol = arp->net_protocol;
118
313
        struct io_buffer *iobuf;
119
314
        struct arphdr *arphdr;
120
315
        int rc;
121
316
 
122
 
        /* Look for existing entry in ARP table */
123
 
        arp = arp_find_entry ( ll_protocol, net_protocol, dest_net_addr );
124
 
        if ( arp ) {
125
 
                DBG ( "ARP cache hit: %s %s => %s %s\n",
126
 
                      net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
127
 
                      ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
128
 
                memcpy ( dest_ll_addr, arp->ll_addr, ll_protocol->ll_addr_len);
129
 
                return 0;
 
317
        /* If we have failed, destroy the cache entry */
 
318
        if ( fail ) {
 
319
                arp_destroy ( arp, -ETIMEDOUT );
 
320
                return;
130
321
        }
131
 
        DBG ( "ARP cache miss: %s %s\n", net_protocol->name,
132
 
              net_protocol->ntoa ( dest_net_addr ) );
 
322
 
 
323
        /* Restart the timer */
 
324
        start_timer ( &arp->timer );
133
325
 
134
326
        /* Allocate ARP packet */
135
327
        iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
136
 
                          2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) );
137
 
        if ( ! iobuf )
138
 
                return -ENOMEM;
 
328
                            ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
 
329
        if ( ! iobuf ) {
 
330
                /* Leave timer running and try again later */
 
331
                return;
 
332
        }
139
333
        iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
140
334
 
141
335
        /* Build up ARP request */
148
342
        memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
149
343
                 netdev->ll_addr, ll_protocol->ll_addr_len );
150
344
        memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
151
 
                 source_net_addr, net_protocol->net_addr_len );
 
345
                 arp->net_source, net_protocol->net_addr_len );
152
346
        memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
153
347
                 0, ll_protocol->ll_addr_len );
154
348
        memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
155
 
                 dest_net_addr, net_protocol->net_addr_len );
 
349
                 arp->net_dest, net_protocol->net_addr_len );
156
350
 
157
351
        /* Transmit ARP request */
158
352
        if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
159
 
                             netdev->ll_broadcast, netdev->ll_addr ) ) != 0 )
160
 
                return rc;
161
 
 
162
 
        return -ENOENT;
 
353
                             netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
 
354
                DBGC ( arp, "ARP %p could not transmit request: %s\n",
 
355
                       arp, strerror ( rc ) );
 
356
                return;
 
357
        }
163
358
}
164
359
 
165
360
/**
188
383
 * @v ll_source         Link-layer source address
189
384
 * @v flags             Packet flags
190
385
 * @ret rc              Return status code
191
 
 *
192
 
 * This handles ARP requests and responses as detailed in RFC826.  The
193
 
 * method detailed within the RFC is pretty optimised, handling
194
 
 * requests and responses with basically a single code path and
195
 
 * avoiding the need for extraneous ARP requests; read the RFC for
196
 
 * details.
197
386
 */
198
387
static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
199
388
                    const void *ll_dest __unused,
204
393
        struct net_protocol *net_protocol;
205
394
        struct ll_protocol *ll_protocol;
206
395
        struct arp_entry *arp;
207
 
        int merge = 0;
 
396
        int rc;
208
397
 
209
398
        /* Identify network-layer and link-layer protocols */
210
399
        arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
211
 
        if ( ! arp_net_protocol )
 
400
        if ( ! arp_net_protocol ) {
 
401
                rc = -EPROTONOSUPPORT;
212
402
                goto done;
 
403
        }
213
404
        net_protocol = arp_net_protocol->net_protocol;
214
405
        ll_protocol = netdev->ll_protocol;
215
406
 
216
407
        /* Sanity checks */
217
408
        if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
218
409
             ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
219
 
             ( arphdr->ar_pln != net_protocol->net_addr_len ) )
 
410
             ( arphdr->ar_pln != net_protocol->net_addr_len ) ) {
 
411
                rc = -EINVAL;
220
412
                goto done;
 
413
        }
221
414
 
222
415
        /* See if we have an entry for this sender, and update it if so */
223
 
        arp = arp_find_entry ( ll_protocol, net_protocol,
224
 
                               arp_sender_pa ( arphdr ) );
 
416
        arp = arp_find ( netdev, net_protocol, arp_sender_pa ( arphdr ) );
225
417
        if ( arp ) {
226
 
                memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
227
 
                         arphdr->ar_hln );
228
 
                merge = 1;
229
 
                DBG ( "ARP cache update: %s %s => %s %s\n",
230
 
                      net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
231
 
                      ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
 
418
                arp_update ( arp, arp_sender_ha ( arphdr ) );
 
419
        }
 
420
 
 
421
        /* If it's not a request, there's nothing more to do */
 
422
        if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
 
423
                rc = 0;
 
424
                goto done;
232
425
        }
233
426
 
234
427
        /* See if we own the target protocol address */
235
 
        if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0)
 
428
        if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
 
429
                rc = 0;
236
430
                goto done;
237
 
        
238
 
        /* Create new ARP table entry if necessary */
239
 
        if ( ! merge ) {
240
 
                arp = &arp_table[next_new_arp_entry++ % NUM_ARP_ENTRIES];
241
 
                arp->ll_protocol = ll_protocol;
242
 
                arp->net_protocol = net_protocol;
243
 
                memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
244
 
                         arphdr->ar_hln );
245
 
                memcpy ( arp->net_addr, arp_sender_pa ( arphdr ),
246
 
                         arphdr->ar_pln);
247
 
                DBG ( "ARP cache add: %s %s => %s %s\n",
248
 
                      net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
249
 
                      ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
250
431
        }
251
432
 
252
 
        /* If it's not a request, there's nothing more to do */
253
 
        if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) )
254
 
                goto done;
255
 
 
256
433
        /* Change request to a reply */
257
 
        DBG ( "ARP reply: %s %s => %s %s\n", net_protocol->name,
258
 
              net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
259
 
              ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
 
434
        DBGC ( netdev, "ARP reply %s %s %s => %s %s\n",
 
435
               netdev->name, net_protocol->name,
 
436
               net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
 
437
               ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
260
438
        arphdr->ar_op = htons ( ARPOP_REPLY );
261
439
        memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
262
440
                 arphdr->ar_hln + arphdr->ar_pln );
263
441
        memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
264
442
 
265
443
        /* Send reply */
266
 
        net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
267
 
                 arp_target_ha ( arphdr ), netdev->ll_addr );
 
444
        if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
 
445
                             arp_target_ha ( arphdr ),
 
446
                             netdev->ll_addr ) ) != 0 ) {
 
447
                DBGC ( netdev, "ARP could not transmit reply via %s: %s\n",
 
448
                       netdev->name, strerror ( rc ) );
 
449
                goto done;
 
450
        }
 
451
 
 
452
        /* Success */
 
453
        rc = 0;
268
454
 
269
455
 done:
270
456
        free_iob ( iobuf );
271
 
        return 0;
 
457
        return rc;
272
458
}
273
459
 
274
460
/**
290
476
        .rx = arp_rx,
291
477
        .ntoa = arp_ntoa,
292
478
};
 
479
 
 
480
/**
 
481
 * Update ARP cache on network device creation
 
482
 *
 
483
 * @v netdev            Network device
 
484
 */
 
485
static int arp_probe ( struct net_device *netdev __unused ) {
 
486
        /* Nothing to do */
 
487
        return 0;
 
488
}
 
489
 
 
490
/**
 
491
 * Update ARP cache on network device state change or removal
 
492
 *
 
493
 * @v netdev            Network device
 
494
 */
 
495
static void arp_flush ( struct net_device *netdev ) {
 
496
        struct arp_entry *arp;
 
497
        struct arp_entry *tmp;
 
498
 
 
499
        /* Remove all ARP cache entries when a network device is closed */
 
500
        if ( ! netdev_is_open ( netdev ) ) {
 
501
                list_for_each_entry_safe ( arp, tmp, &arp_entries, list )
 
502
                        arp_destroy ( arp, -ENODEV );
 
503
        }
 
504
}
 
505
 
 
506
/** ARP driver (for net device notifications) */
 
507
struct net_driver arp_net_driver __net_driver = {
 
508
        .name = "ARP",
 
509
        .probe = arp_probe,
 
510
        .notify = arp_flush,
 
511
        .remove = arp_flush,
 
512
};
 
513
 
 
514
/**
 
515
 * Discard some cached ARP entries
 
516
 *
 
517
 * @ret discarded       Number of cached items discarded
 
518
 */
 
519
static unsigned int arp_discard ( void ) {
 
520
        struct arp_entry *arp;
 
521
 
 
522
        /* Drop oldest cache entry, if any */
 
523
        arp = list_last_entry ( &arp_entries, struct arp_entry, list );
 
524
        if ( arp ) {
 
525
                arp_destroy ( arp, -ENOBUFS );
 
526
                return 1;
 
527
        } else {
 
528
                return 0;
 
529
        }
 
530
}
 
531
 
 
532
/** ARP cache discarder
 
533
 *
 
534
 * ARP cache entries are deemed to have a high replacement cost, since
 
535
 * flushing an active ARP cache entry midway through a TCP transfer
 
536
 * will cause substantial disruption.
 
537
 */
 
538
struct cache_discarder arp_discarder __cache_discarder ( CACHE_EXPENSIVE ) = {
 
539
        .discard = arp_discard,
 
540
};