blob: 941f2a324d3aed7ef68bb2173eb3f562d49a104a [file] [log] [blame]
Patrick McHardyeca9eba2008-07-05 21:26:13 -07001/*
2 * IEEE 802.1D Generic Attribute Registration Protocol (GARP)
3 *
4 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 */
10#include <linux/kernel.h>
11#include <linux/timer.h>
12#include <linux/skbuff.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/rtnetlink.h>
16#include <linux/llc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Patrick McHardyeca9eba2008-07-05 21:26:13 -070018#include <net/llc.h>
19#include <net/llc_pdu.h>
20#include <net/garp.h>
21#include <asm/unaligned.h>
22
23static unsigned int garp_join_time __read_mostly = 200;
24module_param(garp_join_time, uint, 0644);
25MODULE_PARM_DESC(garp_join_time, "Join time in ms (default 200ms)");
26MODULE_LICENSE("GPL");
27
28static const struct garp_state_trans {
29 u8 state;
30 u8 action;
31} garp_applicant_state_table[GARP_APPLICANT_MAX + 1][GARP_EVENT_MAX + 1] = {
32 [GARP_APPLICANT_VA] = {
33 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_AA,
34 .action = GARP_ACTION_S_JOIN_IN },
35 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_AA },
36 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VA },
37 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VA },
38 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VA },
39 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VP },
40 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_INVALID },
41 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_LA },
42 },
43 [GARP_APPLICANT_AA] = {
44 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_QA,
45 .action = GARP_ACTION_S_JOIN_IN },
46 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_QA },
47 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VA },
48 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VA },
49 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VA },
50 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VP },
51 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_INVALID },
52 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_LA },
53 },
54 [GARP_APPLICANT_QA] = {
55 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_INVALID },
56 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_QA },
57 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VA },
58 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VA },
59 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VP },
60 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VP },
61 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_INVALID },
62 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_LA },
63 },
64 [GARP_APPLICANT_LA] = {
65 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_VO,
66 .action = GARP_ACTION_S_LEAVE_EMPTY },
67 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_LA },
68 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VO },
69 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_LA },
70 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_LA },
71 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VO },
72 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_VA },
73 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_INVALID },
74 },
75 [GARP_APPLICANT_VP] = {
76 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_AA,
77 .action = GARP_ACTION_S_JOIN_IN },
78 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_AP },
79 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VP },
80 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VP },
81 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VP },
82 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VP },
83 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_INVALID },
84 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_VO },
85 },
86 [GARP_APPLICANT_AP] = {
87 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_QA,
88 .action = GARP_ACTION_S_JOIN_IN },
89 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_QP },
90 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VP },
91 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VP },
92 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VP },
93 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VP },
94 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_INVALID },
95 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_AO },
96 },
97 [GARP_APPLICANT_QP] = {
98 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_INVALID },
99 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_QP },
100 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VP },
101 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VP },
102 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VP },
103 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VP },
104 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_INVALID },
105 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_QO },
106 },
107 [GARP_APPLICANT_VO] = {
108 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_INVALID },
109 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_AO },
110 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VO },
111 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VO },
112 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VO },
113 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VO },
114 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_VP },
115 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_INVALID },
116 },
117 [GARP_APPLICANT_AO] = {
118 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_INVALID },
119 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_QO },
120 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VO },
121 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VO },
122 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VO },
123 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VO },
124 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_AP },
125 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_INVALID },
126 },
127 [GARP_APPLICANT_QO] = {
128 [GARP_EVENT_TRANSMIT_PDU] = { .state = GARP_APPLICANT_INVALID },
129 [GARP_EVENT_R_JOIN_IN] = { .state = GARP_APPLICANT_QO },
130 [GARP_EVENT_R_JOIN_EMPTY] = { .state = GARP_APPLICANT_VO },
131 [GARP_EVENT_R_EMPTY] = { .state = GARP_APPLICANT_VO },
132 [GARP_EVENT_R_LEAVE_IN] = { .state = GARP_APPLICANT_VO },
133 [GARP_EVENT_R_LEAVE_EMPTY] = { .state = GARP_APPLICANT_VO },
134 [GARP_EVENT_REQ_JOIN] = { .state = GARP_APPLICANT_QP },
135 [GARP_EVENT_REQ_LEAVE] = { .state = GARP_APPLICANT_INVALID },
136 },
137};
138
139static int garp_attr_cmp(const struct garp_attr *attr,
140 const void *data, u8 len, u8 type)
141{
142 if (attr->type != type)
143 return attr->type - type;
144 if (attr->dlen != len)
145 return attr->dlen - len;
146 return memcmp(attr->data, data, len);
147}
148
149static struct garp_attr *garp_attr_lookup(const struct garp_applicant *app,
150 const void *data, u8 len, u8 type)
151{
152 struct rb_node *parent = app->gid.rb_node;
153 struct garp_attr *attr;
154 int d;
155
156 while (parent) {
157 attr = rb_entry(parent, struct garp_attr, node);
158 d = garp_attr_cmp(attr, data, len, type);
159 if (d < 0)
160 parent = parent->rb_left;
161 else if (d > 0)
162 parent = parent->rb_right;
163 else
164 return attr;
165 }
166 return NULL;
167}
168
169static void garp_attr_insert(struct garp_applicant *app, struct garp_attr *new)
170{
171 struct rb_node *parent = NULL, **p = &app->gid.rb_node;
172 struct garp_attr *attr;
173 int d;
174
175 while (*p) {
176 parent = *p;
177 attr = rb_entry(parent, struct garp_attr, node);
178 d = garp_attr_cmp(attr, new->data, new->dlen, new->type);
179 if (d < 0)
180 p = &parent->rb_left;
181 else if (d > 0)
182 p = &parent->rb_right;
183 }
184 rb_link_node(&new->node, parent, p);
185 rb_insert_color(&new->node, &app->gid);
186}
187
188static struct garp_attr *garp_attr_create(struct garp_applicant *app,
189 const void *data, u8 len, u8 type)
190{
191 struct garp_attr *attr;
192
193 attr = kmalloc(sizeof(*attr) + len, GFP_ATOMIC);
194 if (!attr)
195 return attr;
196 attr->state = GARP_APPLICANT_VO;
197 attr->type = type;
198 attr->dlen = len;
199 memcpy(attr->data, data, len);
200 garp_attr_insert(app, attr);
201 return attr;
202}
203
204static void garp_attr_destroy(struct garp_applicant *app, struct garp_attr *attr)
205{
206 rb_erase(&attr->node, &app->gid);
207 kfree(attr);
208}
209
210static int garp_pdu_init(struct garp_applicant *app)
211{
212 struct sk_buff *skb;
213 struct garp_pdu_hdr *gp;
214
215#define LLC_RESERVE sizeof(struct llc_pdu_un)
216 skb = alloc_skb(app->dev->mtu + LL_RESERVED_SPACE(app->dev),
217 GFP_ATOMIC);
218 if (!skb)
219 return -ENOMEM;
220
221 skb->dev = app->dev;
222 skb->protocol = htons(ETH_P_802_2);
223 skb_reserve(skb, LL_RESERVED_SPACE(app->dev) + LLC_RESERVE);
224
225 gp = (struct garp_pdu_hdr *)__skb_put(skb, sizeof(*gp));
226 put_unaligned(htons(GARP_PROTOCOL_ID), &gp->protocol);
227
228 app->pdu = skb;
229 return 0;
230}
231
232static int garp_pdu_append_end_mark(struct garp_applicant *app)
233{
234 if (skb_tailroom(app->pdu) < sizeof(u8))
235 return -1;
236 *(u8 *)__skb_put(app->pdu, sizeof(u8)) = GARP_END_MARK;
237 return 0;
238}
239
240static void garp_pdu_queue(struct garp_applicant *app)
241{
242 if (!app->pdu)
243 return;
244
245 garp_pdu_append_end_mark(app);
246 garp_pdu_append_end_mark(app);
247
248 llc_pdu_header_init(app->pdu, LLC_PDU_TYPE_U, LLC_SAP_BSPAN,
249 LLC_SAP_BSPAN, LLC_PDU_CMD);
250 llc_pdu_init_as_ui_cmd(app->pdu);
251 llc_mac_hdr_init(app->pdu, app->dev->dev_addr,
252 app->app->proto.group_address);
253
254 skb_queue_tail(&app->queue, app->pdu);
255 app->pdu = NULL;
256}
257
258static void garp_queue_xmit(struct garp_applicant *app)
259{
260 struct sk_buff *skb;
261
262 while ((skb = skb_dequeue(&app->queue)))
263 dev_queue_xmit(skb);
264}
265
266static int garp_pdu_append_msg(struct garp_applicant *app, u8 attrtype)
267{
268 struct garp_msg_hdr *gm;
269
270 if (skb_tailroom(app->pdu) < sizeof(*gm))
271 return -1;
272 gm = (struct garp_msg_hdr *)__skb_put(app->pdu, sizeof(*gm));
273 gm->attrtype = attrtype;
274 garp_cb(app->pdu)->cur_type = attrtype;
275 return 0;
276}
277
278static int garp_pdu_append_attr(struct garp_applicant *app,
279 const struct garp_attr *attr,
280 enum garp_attr_event event)
281{
282 struct garp_attr_hdr *ga;
283 unsigned int len;
284 int err;
285again:
286 if (!app->pdu) {
287 err = garp_pdu_init(app);
288 if (err < 0)
289 return err;
290 }
291
292 if (garp_cb(app->pdu)->cur_type != attr->type) {
293 if (garp_cb(app->pdu)->cur_type &&
294 garp_pdu_append_end_mark(app) < 0)
295 goto queue;
296 if (garp_pdu_append_msg(app, attr->type) < 0)
297 goto queue;
298 }
299
300 len = sizeof(*ga) + attr->dlen;
301 if (skb_tailroom(app->pdu) < len)
302 goto queue;
303 ga = (struct garp_attr_hdr *)__skb_put(app->pdu, len);
304 ga->len = len;
305 ga->event = event;
306 memcpy(ga->data, attr->data, attr->dlen);
307 return 0;
308
309queue:
310 garp_pdu_queue(app);
311 goto again;
312}
313
314static void garp_attr_event(struct garp_applicant *app,
315 struct garp_attr *attr, enum garp_event event)
316{
317 enum garp_applicant_state state;
318
319 state = garp_applicant_state_table[attr->state][event].state;
320 if (state == GARP_APPLICANT_INVALID)
321 return;
322
323 switch (garp_applicant_state_table[attr->state][event].action) {
324 case GARP_ACTION_NONE:
325 break;
326 case GARP_ACTION_S_JOIN_IN:
Patrick McHardy51ce7ec2008-07-16 20:51:47 -0700327 /* When appending the attribute fails, don't update state in
328 * order to retry on next TRANSMIT_PDU event. */
329 if (garp_pdu_append_attr(app, attr, GARP_JOIN_IN) < 0)
330 return;
Patrick McHardyeca9eba2008-07-05 21:26:13 -0700331 break;
332 case GARP_ACTION_S_LEAVE_EMPTY:
333 garp_pdu_append_attr(app, attr, GARP_LEAVE_EMPTY);
334 /* As a pure applicant, sending a leave message implies that
335 * the attribute was unregistered and can be destroyed. */
336 garp_attr_destroy(app, attr);
337 return;
338 default:
339 WARN_ON(1);
340 }
341
342 attr->state = state;
343}
344
345int garp_request_join(const struct net_device *dev,
346 const struct garp_application *appl,
347 const void *data, u8 len, u8 type)
348{
349 struct garp_port *port = dev->garp_port;
350 struct garp_applicant *app = port->applicants[appl->type];
351 struct garp_attr *attr;
352
353 spin_lock_bh(&app->lock);
354 attr = garp_attr_create(app, data, len, type);
355 if (!attr) {
356 spin_unlock_bh(&app->lock);
357 return -ENOMEM;
358 }
359 garp_attr_event(app, attr, GARP_EVENT_REQ_JOIN);
360 spin_unlock_bh(&app->lock);
361 return 0;
362}
363EXPORT_SYMBOL_GPL(garp_request_join);
364
365void garp_request_leave(const struct net_device *dev,
366 const struct garp_application *appl,
367 const void *data, u8 len, u8 type)
368{
369 struct garp_port *port = dev->garp_port;
370 struct garp_applicant *app = port->applicants[appl->type];
371 struct garp_attr *attr;
372
373 spin_lock_bh(&app->lock);
374 attr = garp_attr_lookup(app, data, len, type);
375 if (!attr) {
376 spin_unlock_bh(&app->lock);
377 return;
378 }
379 garp_attr_event(app, attr, GARP_EVENT_REQ_LEAVE);
380 spin_unlock_bh(&app->lock);
381}
382EXPORT_SYMBOL_GPL(garp_request_leave);
383
384static void garp_gid_event(struct garp_applicant *app, enum garp_event event)
385{
386 struct rb_node *node, *next;
387 struct garp_attr *attr;
388
389 for (node = rb_first(&app->gid);
390 next = node ? rb_next(node) : NULL, node != NULL;
391 node = next) {
392 attr = rb_entry(node, struct garp_attr, node);
393 garp_attr_event(app, attr, event);
394 }
395}
396
397static void garp_join_timer_arm(struct garp_applicant *app)
398{
399 unsigned long delay;
400
401 delay = (u64)msecs_to_jiffies(garp_join_time) * net_random() >> 32;
402 mod_timer(&app->join_timer, jiffies + delay);
403}
404
405static void garp_join_timer(unsigned long data)
406{
407 struct garp_applicant *app = (struct garp_applicant *)data;
408
409 spin_lock(&app->lock);
410 garp_gid_event(app, GARP_EVENT_TRANSMIT_PDU);
411 garp_pdu_queue(app);
412 spin_unlock(&app->lock);
413
414 garp_queue_xmit(app);
415 garp_join_timer_arm(app);
416}
417
418static int garp_pdu_parse_end_mark(struct sk_buff *skb)
419{
420 if (!pskb_may_pull(skb, sizeof(u8)))
421 return -1;
422 if (*skb->data == GARP_END_MARK) {
423 skb_pull(skb, sizeof(u8));
424 return -1;
425 }
426 return 0;
427}
428
429static int garp_pdu_parse_attr(struct garp_applicant *app, struct sk_buff *skb,
430 u8 attrtype)
431{
432 const struct garp_attr_hdr *ga;
433 struct garp_attr *attr;
434 enum garp_event event;
435 unsigned int dlen;
436
437 if (!pskb_may_pull(skb, sizeof(*ga)))
438 return -1;
439 ga = (struct garp_attr_hdr *)skb->data;
440 if (ga->len < sizeof(*ga))
441 return -1;
442
443 if (!pskb_may_pull(skb, ga->len))
444 return -1;
445 skb_pull(skb, ga->len);
446 dlen = sizeof(*ga) - ga->len;
447
448 if (attrtype > app->app->maxattr)
449 return 0;
450
451 switch (ga->event) {
452 case GARP_LEAVE_ALL:
453 if (dlen != 0)
454 return -1;
455 garp_gid_event(app, GARP_EVENT_R_LEAVE_EMPTY);
456 return 0;
457 case GARP_JOIN_EMPTY:
458 event = GARP_EVENT_R_JOIN_EMPTY;
459 break;
460 case GARP_JOIN_IN:
461 event = GARP_EVENT_R_JOIN_IN;
462 break;
463 case GARP_LEAVE_EMPTY:
464 event = GARP_EVENT_R_LEAVE_EMPTY;
465 break;
466 case GARP_EMPTY:
467 event = GARP_EVENT_R_EMPTY;
468 break;
469 default:
470 return 0;
471 }
472
473 if (dlen == 0)
474 return -1;
475 attr = garp_attr_lookup(app, ga->data, dlen, attrtype);
476 if (attr == NULL)
477 return 0;
478 garp_attr_event(app, attr, event);
479 return 0;
480}
481
482static int garp_pdu_parse_msg(struct garp_applicant *app, struct sk_buff *skb)
483{
484 const struct garp_msg_hdr *gm;
485
486 if (!pskb_may_pull(skb, sizeof(*gm)))
487 return -1;
488 gm = (struct garp_msg_hdr *)skb->data;
489 if (gm->attrtype == 0)
490 return -1;
491 skb_pull(skb, sizeof(*gm));
492
493 while (skb->len > 0) {
494 if (garp_pdu_parse_attr(app, skb, gm->attrtype) < 0)
495 return -1;
496 if (garp_pdu_parse_end_mark(skb) < 0)
497 break;
498 }
499 return 0;
500}
501
502static void garp_pdu_rcv(const struct stp_proto *proto, struct sk_buff *skb,
503 struct net_device *dev)
504{
505 struct garp_application *appl = proto->data;
506 struct garp_port *port;
507 struct garp_applicant *app;
508 const struct garp_pdu_hdr *gp;
509
510 port = rcu_dereference(dev->garp_port);
511 if (!port)
512 goto err;
513 app = rcu_dereference(port->applicants[appl->type]);
514 if (!app)
515 goto err;
516
517 if (!pskb_may_pull(skb, sizeof(*gp)))
518 goto err;
519 gp = (struct garp_pdu_hdr *)skb->data;
520 if (get_unaligned(&gp->protocol) != htons(GARP_PROTOCOL_ID))
521 goto err;
522 skb_pull(skb, sizeof(*gp));
523
524 spin_lock(&app->lock);
525 while (skb->len > 0) {
526 if (garp_pdu_parse_msg(app, skb) < 0)
527 break;
528 if (garp_pdu_parse_end_mark(skb) < 0)
529 break;
530 }
531 spin_unlock(&app->lock);
532err:
533 kfree_skb(skb);
534}
535
536static int garp_init_port(struct net_device *dev)
537{
538 struct garp_port *port;
539
540 port = kzalloc(sizeof(*port), GFP_KERNEL);
541 if (!port)
542 return -ENOMEM;
543 rcu_assign_pointer(dev->garp_port, port);
544 return 0;
545}
546
547static void garp_release_port(struct net_device *dev)
548{
549 struct garp_port *port = dev->garp_port;
550 unsigned int i;
551
552 for (i = 0; i <= GARP_APPLICATION_MAX; i++) {
553 if (port->applicants[i])
554 return;
555 }
556 rcu_assign_pointer(dev->garp_port, NULL);
557 synchronize_rcu();
558 kfree(port);
559}
560
561int garp_init_applicant(struct net_device *dev, struct garp_application *appl)
562{
563 struct garp_applicant *app;
564 int err;
565
566 ASSERT_RTNL();
567
568 if (!dev->garp_port) {
569 err = garp_init_port(dev);
570 if (err < 0)
571 goto err1;
572 }
573
574 err = -ENOMEM;
575 app = kzalloc(sizeof(*app), GFP_KERNEL);
576 if (!app)
577 goto err2;
578
Jiri Pirko22bedad2010-04-01 21:22:57 +0000579 err = dev_mc_add(dev, appl->proto.group_address);
Patrick McHardyeca9eba2008-07-05 21:26:13 -0700580 if (err < 0)
581 goto err3;
582
583 app->dev = dev;
584 app->app = appl;
585 app->gid = RB_ROOT;
586 spin_lock_init(&app->lock);
587 skb_queue_head_init(&app->queue);
588 rcu_assign_pointer(dev->garp_port->applicants[appl->type], app);
589 setup_timer(&app->join_timer, garp_join_timer, (unsigned long)app);
590 garp_join_timer_arm(app);
591 return 0;
592
593err3:
594 kfree(app);
595err2:
596 garp_release_port(dev);
597err1:
598 return err;
599}
600EXPORT_SYMBOL_GPL(garp_init_applicant);
601
602void garp_uninit_applicant(struct net_device *dev, struct garp_application *appl)
603{
604 struct garp_port *port = dev->garp_port;
605 struct garp_applicant *app = port->applicants[appl->type];
606
607 ASSERT_RTNL();
608
609 rcu_assign_pointer(port->applicants[appl->type], NULL);
610 synchronize_rcu();
611
612 /* Delete timer and generate a final TRANSMIT_PDU event to flush out
613 * all pending messages before the applicant is gone. */
614 del_timer_sync(&app->join_timer);
615 garp_gid_event(app, GARP_EVENT_TRANSMIT_PDU);
616 garp_pdu_queue(app);
617 garp_queue_xmit(app);
618
Jiri Pirko22bedad2010-04-01 21:22:57 +0000619 dev_mc_del(dev, appl->proto.group_address);
Patrick McHardyeca9eba2008-07-05 21:26:13 -0700620 kfree(app);
621 garp_release_port(dev);
622}
623EXPORT_SYMBOL_GPL(garp_uninit_applicant);
624
625int garp_register_application(struct garp_application *appl)
626{
627 appl->proto.rcv = garp_pdu_rcv;
628 appl->proto.data = appl;
629 return stp_proto_register(&appl->proto);
630}
631EXPORT_SYMBOL_GPL(garp_register_application);
632
633void garp_unregister_application(struct garp_application *appl)
634{
635 stp_proto_unregister(&appl->proto);
636}
637EXPORT_SYMBOL_GPL(garp_unregister_application);