1 /*
2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <byteswap.h>
31 #include <ipxe/features.h>
32 #include <ipxe/if_ether.h>
33 #include <ipxe/ethernet.h>
34 #include <ipxe/netdevice.h>
35 #include <ipxe/iobuf.h>
36 #include <ipxe/vlan.h>
37
38 /** @file
39 *
40 * Virtual LANs
41 *
42 */
43
44 FEATURE ( FEATURE_PROTOCOL, "VLAN", DHCP_EB_FEATURE_VLAN, 1 );
45
46 struct net_protocol vlan_protocol __net_protocol;
47
48 /** VLAN device private data */
49 struct vlan_device {
50 /** Trunk network device */
51 struct net_device *trunk;
52 /** VLAN tag */
53 unsigned int tag;
54 /** Default priority */
55 unsigned int priority;
56 };
57
58 /**
59 * Open VLAN device
60 *
61 * @v netdev Network device
62 * @ret rc Return status code
63 */
vlan_open(struct net_device * netdev)64 static int vlan_open ( struct net_device *netdev ) {
65 struct vlan_device *vlan = netdev->priv;
66
67 return netdev_open ( vlan->trunk );
68 }
69
70 /**
71 * Close VLAN device
72 *
73 * @v netdev Network device
74 */
vlan_close(struct net_device * netdev)75 static void vlan_close ( struct net_device *netdev ) {
76 struct vlan_device *vlan = netdev->priv;
77
78 netdev_close ( vlan->trunk );
79 }
80
81 /**
82 * Transmit packet on VLAN device
83 *
84 * @v netdev Network device
85 * @v iobuf I/O buffer
86 * @ret rc Return status code
87 */
vlan_transmit(struct net_device * netdev,struct io_buffer * iobuf)88 static int vlan_transmit ( struct net_device *netdev,
89 struct io_buffer *iobuf ) {
90 struct vlan_device *vlan = netdev->priv;
91 struct net_device *trunk = vlan->trunk;
92 struct ll_protocol *ll_protocol;
93 struct vlan_header *vlanhdr;
94 uint8_t ll_dest_copy[ETH_ALEN];
95 uint8_t ll_source_copy[ETH_ALEN];
96 const void *ll_dest;
97 const void *ll_source;
98 uint16_t net_proto;
99 unsigned int flags;
100 int rc;
101
102 /* Strip link-layer header and preserve link-layer header fields */
103 ll_protocol = netdev->ll_protocol;
104 if ( ( rc = ll_protocol->pull ( netdev, iobuf, &ll_dest, &ll_source,
105 &net_proto, &flags ) ) != 0 ) {
106 DBGC ( netdev, "VLAN %s could not parse link-layer header: "
107 "%s\n", netdev->name, strerror ( rc ) );
108 return rc;
109 }
110 memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
111 memcpy ( ll_source_copy, ll_source, ETH_ALEN );
112
113 /* Construct VLAN header */
114 vlanhdr = iob_push ( iobuf, sizeof ( *vlanhdr ) );
115 vlanhdr->tci = htons ( VLAN_TCI ( vlan->tag, vlan->priority ) );
116 vlanhdr->net_proto = net_proto;
117
118 /* Reclaim I/O buffer from VLAN device's TX queue */
119 list_del ( &iobuf->list );
120
121 /* Transmit packet on trunk device */
122 if ( ( rc = net_tx ( iob_disown ( iobuf ), trunk, &vlan_protocol,
123 ll_dest_copy, ll_source_copy ) ) != 0 ) {
124 DBGC ( netdev, "VLAN %s could not transmit: %s\n",
125 netdev->name, strerror ( rc ) );
126 /* Cannot return an error status, since that would
127 * cause the I/O buffer to be double-freed.
128 */
129 return 0;
130 }
131
132 return 0;
133 }
134
135 /**
136 * Poll VLAN device
137 *
138 * @v netdev Network device
139 */
vlan_poll(struct net_device * netdev)140 static void vlan_poll ( struct net_device *netdev ) {
141 struct vlan_device *vlan = netdev->priv;
142
143 /* Poll trunk device */
144 netdev_poll ( vlan->trunk );
145 }
146
147 /**
148 * Enable/disable interrupts on VLAN device
149 *
150 * @v netdev Network device
151 * @v enable Interrupts should be enabled
152 */
vlan_irq(struct net_device * netdev,int enable)153 static void vlan_irq ( struct net_device *netdev, int enable ) {
154 struct vlan_device *vlan = netdev->priv;
155
156 /* Enable/disable interrupts on trunk device. This is not at
157 * all robust, but there is no sensible course of action
158 * available.
159 */
160 netdev_irq ( vlan->trunk, enable );
161 }
162
163 /** VLAN device operations */
164 static struct net_device_operations vlan_operations = {
165 .open = vlan_open,
166 .close = vlan_close,
167 .transmit = vlan_transmit,
168 .poll = vlan_poll,
169 .irq = vlan_irq,
170 };
171
172 /**
173 * Synchronise VLAN device
174 *
175 * @v netdev Network device
176 */
vlan_sync(struct net_device * netdev)177 static void vlan_sync ( struct net_device *netdev ) {
178 struct vlan_device *vlan = netdev->priv;
179 struct net_device *trunk = vlan->trunk;
180
181 /* Synchronise link status */
182 if ( netdev->link_rc != trunk->link_rc )
183 netdev_link_err ( netdev, trunk->link_rc );
184
185 /* Synchronise open/closed status */
186 if ( netdev_is_open ( trunk ) ) {
187 if ( ! netdev_is_open ( netdev ) )
188 netdev_open ( netdev );
189 } else {
190 if ( netdev_is_open ( netdev ) )
191 netdev_close ( netdev );
192 }
193 }
194
195 /**
196 * Identify VLAN device
197 *
198 * @v trunk Trunk network device
199 * @v tag VLAN tag
200 * @ret netdev VLAN device, if any
201 */
vlan_find(struct net_device * trunk,unsigned int tag)202 static struct net_device * vlan_find ( struct net_device *trunk,
203 unsigned int tag ) {
204 struct net_device *netdev;
205 struct vlan_device *vlan;
206
207 for_each_netdev ( netdev ) {
208 if ( netdev->op != &vlan_operations )
209 continue;
210 vlan = netdev->priv;
211 if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
212 return netdev;
213 }
214 return NULL;
215 }
216
217 /**
218 * Process incoming VLAN packet
219 *
220 * @v iobuf I/O buffer
221 * @v trunk Trunk network device
222 * @v ll_dest Link-layer destination address
223 * @v ll_source Link-layer source address
224 * @v flags Packet flags
225 * @ret rc Return status code
226 */
vlan_rx(struct io_buffer * iobuf,struct net_device * trunk,const void * ll_dest,const void * ll_source,unsigned int flags __unused)227 static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
228 const void *ll_dest, const void *ll_source,
229 unsigned int flags __unused ) {
230 struct vlan_header *vlanhdr = iobuf->data;
231 struct net_device *netdev;
232 struct ll_protocol *ll_protocol;
233 uint8_t ll_dest_copy[ETH_ALEN];
234 uint8_t ll_source_copy[ETH_ALEN];
235 uint16_t tag;
236 int rc;
237
238 /* Sanity check */
239 if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
240 DBGC ( trunk, "VLAN %s received underlength packet (%zd "
241 "bytes)\n", trunk->name, iob_len ( iobuf ) );
242 rc = -EINVAL;
243 goto err_sanity;
244 }
245
246 /* Identify VLAN device */
247 tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
248 netdev = vlan_find ( trunk, tag );
249 if ( ! netdev ) {
250 DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
251 "%d\n", trunk->name, tag );
252 rc = -EPIPE;
253 goto err_no_vlan;
254 }
255
256 /* Strip VLAN header and preserve original link-layer header fields */
257 iob_pull ( iobuf, sizeof ( *vlanhdr ) );
258 ll_protocol = trunk->ll_protocol;
259 memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
260 memcpy ( ll_source_copy, ll_source, ETH_ALEN );
261
262 /* Reconstruct link-layer header for VLAN device */
263 ll_protocol = netdev->ll_protocol;
264 if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
265 ll_source_copy,
266 vlanhdr->net_proto ) ) != 0 ) {
267 DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
268 "header: %s\n", netdev->name, strerror ( rc ) );
269 goto err_ll_push;
270 }
271
272 /* Enqueue packet on VLAN device */
273 netdev_rx ( netdev, iob_disown ( iobuf ) );
274 return 0;
275
276 err_ll_push:
277 err_no_vlan:
278 err_sanity:
279 free_iob ( iobuf );
280 return rc;
281 }
282
283 /** VLAN protocol */
284 struct net_protocol vlan_protocol __net_protocol = {
285 .name = "VLAN",
286 .net_proto = htons ( ETH_P_8021Q ),
287 .rx = vlan_rx,
288 };
289
290 /**
291 * Get the VLAN tag
292 *
293 * @v netdev Network device
294 * @ret tag VLAN tag, or 0 if device is not a VLAN device
295 */
vlan_tag(struct net_device * netdev)296 unsigned int vlan_tag ( struct net_device *netdev ) {
297 struct vlan_device *vlan;
298
299 if ( netdev->op == &vlan_operations ) {
300 vlan = netdev->priv;
301 return vlan->tag;
302 } else {
303 return 0;
304 }
305 }
306
307 /**
308 * Check if network device can be used as a VLAN trunk device
309 *
310 * @v trunk Trunk network device
311 * @ret is_ok Trunk network device is usable
312 *
313 * VLAN devices will be created as Ethernet devices. (We cannot
314 * simply clone the link layer of the trunk network device, because
315 * this link layer may expect the network device structure to contain
316 * some link-layer-private data.) The trunk network device must
317 * therefore have a link layer that is in some sense 'compatible' with
318 * Ethernet; specifically, it must have link-layer addresses that are
319 * the same length as Ethernet link-layer addresses.
320 *
321 * As an additional check, and primarily to assist with the sanity of
322 * the FCoE code, we refuse to allow nested VLANs.
323 */
vlan_can_be_trunk(struct net_device * trunk)324 int vlan_can_be_trunk ( struct net_device *trunk ) {
325
326 return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
327 ( trunk->op != &vlan_operations ) );
328 }
329
330 /**
331 * Create VLAN device
332 *
333 * @v trunk Trunk network device
334 * @v tag VLAN tag
335 * @v priority Default VLAN priority
336 * @ret rc Return status code
337 */
vlan_create(struct net_device * trunk,unsigned int tag,unsigned int priority)338 int vlan_create ( struct net_device *trunk, unsigned int tag,
339 unsigned int priority ) {
340 struct net_device *netdev;
341 struct vlan_device *vlan;
342 int rc;
343
344 /* If VLAN already exists, just update the priority */
345 if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
346 vlan = netdev->priv;
347 if ( priority != vlan->priority ) {
348 DBGC ( netdev, "VLAN %s priority changed from %d to "
349 "%d\n", netdev->name, vlan->priority, priority );
350 }
351 vlan->priority = priority;
352 return 0;
353 }
354
355 /* Sanity checks */
356 if ( ! vlan_can_be_trunk ( trunk ) ) {
357 DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
358 "device\n", trunk->name );
359 rc = -ENOTTY;
360 goto err_sanity;
361 }
362 if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
363 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
364 "%d\n", trunk->name, tag );
365 rc = -EINVAL;
366 goto err_sanity;
367 }
368 if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
369 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
370 "priority %d\n", trunk->name, priority );
371 rc = -EINVAL;
372 goto err_sanity;
373 }
374
375 /* Allocate and initialise structure */
376 netdev = alloc_etherdev ( sizeof ( *vlan ) );
377 if ( ! netdev ) {
378 rc = -ENOMEM;
379 goto err_alloc_etherdev;
380 }
381 netdev_init ( netdev, &vlan_operations );
382 netdev->dev = trunk->dev;
383 memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
384 vlan = netdev->priv;
385 vlan->trunk = netdev_get ( trunk );
386 vlan->tag = tag;
387 vlan->priority = priority;
388
389 /* Construct VLAN device name */
390 snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
391 trunk->name, vlan->tag );
392
393 /* Mark device as not supporting interrupts, if applicable */
394 if ( ! netdev_irq_supported ( trunk ) )
395 netdev->state |= NETDEV_IRQ_UNSUPPORTED;
396
397 /* Register VLAN device */
398 if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
399 DBGC ( netdev, "VLAN %s could not register: %s\n",
400 netdev->name, strerror ( rc ) );
401 goto err_register;
402 }
403
404 /* Synchronise with trunk device */
405 vlan_sync ( netdev );
406
407 DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
408 netdev->name, vlan->tag, vlan->priority );
409
410 return 0;
411
412 unregister_netdev ( netdev );
413 err_register:
414 netdev_nullify ( netdev );
415 netdev_put ( netdev );
416 netdev_put ( trunk );
417 err_alloc_etherdev:
418 err_sanity:
419 return rc;
420 }
421
422 /**
423 * Destroy VLAN device
424 *
425 * @v netdev Network device
426 * @ret rc Return status code
427 */
vlan_destroy(struct net_device * netdev)428 int vlan_destroy ( struct net_device *netdev ) {
429 struct vlan_device *vlan = netdev->priv;
430 struct net_device *trunk;
431
432 /* Sanity check */
433 if ( netdev->op != &vlan_operations ) {
434 DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
435 netdev->name );
436 return -ENOTTY;
437 }
438
439 DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
440
441 /* Remove VLAN device */
442 unregister_netdev ( netdev );
443 trunk = vlan->trunk;
444 netdev_nullify ( netdev );
445 netdev_put ( netdev );
446 netdev_put ( trunk );
447
448 return 0;
449 }
450
451 /**
452 * Handle trunk network device link state change
453 *
454 * @v trunk Trunk network device
455 */
vlan_notify(struct net_device * trunk)456 static void vlan_notify ( struct net_device *trunk ) {
457 struct net_device *netdev;
458 struct vlan_device *vlan;
459
460 for_each_netdev ( netdev ) {
461 if ( netdev->op != &vlan_operations )
462 continue;
463 vlan = netdev->priv;
464 if ( vlan->trunk == trunk )
465 vlan_sync ( netdev );
466 }
467 }
468
469 /**
470 * Destroy first VLAN device for a given trunk
471 *
472 * @v trunk Trunk network device
473 * @ret found A VLAN device was found
474 */
vlan_remove_first(struct net_device * trunk)475 static int vlan_remove_first ( struct net_device *trunk ) {
476 struct net_device *netdev;
477 struct vlan_device *vlan;
478
479 for_each_netdev ( netdev ) {
480 if ( netdev->op != &vlan_operations )
481 continue;
482 vlan = netdev->priv;
483 if ( vlan->trunk == trunk ) {
484 vlan_destroy ( netdev );
485 return 1;
486 }
487 }
488 return 0;
489 }
490
491 /**
492 * Destroy all VLAN devices for a given trunk
493 *
494 * @v trunk Trunk network device
495 */
vlan_remove(struct net_device * trunk)496 static void vlan_remove ( struct net_device *trunk ) {
497
498 /* Remove all VLAN devices attached to this trunk, safe
499 * against arbitrary net device removal.
500 */
501 while ( vlan_remove_first ( trunk ) ) {}
502 }
503
504 /** VLAN driver */
505 struct net_driver vlan_driver __net_driver = {
506 .name = "VLAN",
507 .notify = vlan_notify,
508 .remove = vlan_remove,
509 };
510
511 /**
512 * Add VLAN tag-stripped packet to receive queue
513 *
514 * @v netdev Network device
515 * @v tag VLAN tag, or zero
516 * @v iobuf I/O buffer
517 */
vlan_netdev_rx(struct net_device * netdev,unsigned int tag,struct io_buffer * iobuf)518 void vlan_netdev_rx ( struct net_device *netdev, unsigned int tag,
519 struct io_buffer *iobuf ) {
520 struct net_device *vlan;
521
522 /* Identify VLAN device, if applicable */
523 if ( tag ) {
524 if ( ( vlan = vlan_find ( netdev, tag ) ) == NULL ) {
525 netdev_rx_err ( netdev, iobuf, -ENODEV );
526 return;
527 }
528 netdev = vlan;
529 }
530
531 /* Hand off to network device */
532 netdev_rx ( netdev, iobuf );
533 }
534
535 /**
536 * Discard received VLAN tag-stripped packet
537 *
538 * @v netdev Network device
539 * @v tag VLAN tag, or zero
540 * @v iobuf I/O buffer, or NULL
541 * @v rc Packet status code
542 */
vlan_netdev_rx_err(struct net_device * netdev,unsigned int tag,struct io_buffer * iobuf,int rc)543 void vlan_netdev_rx_err ( struct net_device *netdev, unsigned int tag,
544 struct io_buffer *iobuf, int rc ) {
545 struct net_device *vlan;
546
547 /* Identify VLAN device, if applicable */
548 if ( tag && ( ( vlan = vlan_find ( netdev, tag ) ) != NULL ) )
549 netdev = vlan;
550
551 /* Hand off to network device */
552 netdev_rx_err ( netdev, iobuf, rc );
553 }
554