1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/rwlock.h>
#include <sys/errno.h>
#include <sys/queue.h>
#include <inet/common.h>
#include <inet/led.h>
#include <inet/ip.h>
#include <sys/neti.h>
#include <sys/zone.h>
static net_handle_t net_find(const char *protocol, neti_stack_t *ns);
static net_handle_t
net_find(const char *protocol, neti_stack_t *nts)
{
struct net_data *n;
ASSERT(protocol != NULL);
ASSERT(nts != NULL);
LIST_FOREACH(n, &nts->nts_netd_head, netd_list) {
ASSERT(n->netd_info.netp_name != NULL);
/*
* If they're trying to find a protocol that is being
* shutdown, just ignore it..
*/
if (n->netd_condemned != 0)
continue;
if (strcmp(n->netd_info.netp_name, protocol) == 0) {
break;
}
}
return (n);
}
net_handle_t
net_protocol_register(netid_t id, const net_protocol_t *info)
{
struct net_data *n, *new;
neti_stack_t *nts;
ASSERT(info != NULL);
nts = net_getnetistackbyid(id);
if (nts == NULL)
return (NULL);
new = kmem_alloc(sizeof (*new), KM_SLEEP);
new->netd_refcnt = 1;
new->netd_hooks = NULL;
new->netd_info = *info;
new->netd_stack = nts;
new->netd_condemned = 0;
mutex_enter(&nts->nts_lock);
n = net_find(info->netp_name, nts);
if (n != NULL) {
mutex_exit(&nts->nts_lock);
kmem_free(new, sizeof (*new));
return (NULL);
}
if (LIST_EMPTY(&nts->nts_netd_head)) {
LIST_INSERT_HEAD(&nts->nts_netd_head, new, netd_list);
} else {
LIST_INSERT_AFTER(LIST_FIRST(&nts->nts_netd_head),
new, netd_list);
}
mutex_exit(&nts->nts_lock);
return (new);
}
int
net_protocol_unregister(net_handle_t info)
{
neti_stack_t *nts;
ASSERT(info != NULL);
nts = info->netd_stack;
ASSERT(nts != NULL);
mutex_enter(&nts->nts_lock);
LIST_REMOVE(info, netd_list);
info->netd_stack = NULL;
mutex_exit(&nts->nts_lock);
(void) net_protocol_release(info);
return (0);
}
net_handle_t
net_protocol_lookup(netid_t netid, const char *protocol)
{
neti_stack_t *nts;
net_handle_t nd;
ASSERT(protocol != NULL);
nts = net_getnetistackbyid(netid);
if (nts == NULL)
return (NULL);
mutex_enter(&nts->nts_lock);
nd = net_find(protocol, nts);
if (nd != NULL)
atomic_inc_32((uint_t *)&nd->netd_refcnt);
mutex_exit(&nts->nts_lock);
return (nd);
}
/*
* Note: the man page specifies "returns -1 if the value passed in is unknown
* to this framework". We are not doing a lookup in this function, just a
* simply add to the netd_refcnt of the net_handle_t passed in, so -1 is never a
* return value.
*/
int
net_protocol_release(net_handle_t info)
{
ASSERT(info->netd_refcnt > 0);
/*
* Is this safe? No hold on nts_lock? Consider that if the caller
* of net_protocol_release() is going to free this structure then
* it is now the only owner (refcnt==1) and it will have been
* removed from the nts_netd_head list on the neti_stack_t from a
* call to net_protocol_unregister already, so it is thus an orphan.
*/
if (atomic_dec_32_nv((uint_t *)&info->netd_refcnt) == 0) {
ASSERT(info->netd_hooks == NULL);
ASSERT(info->netd_stack == NULL);
kmem_free(info, sizeof (struct net_data));
}
return (0);
}
net_handle_t
net_protocol_walk(netid_t netid, net_handle_t info)
{
struct net_data *n = NULL;
boolean_t found = B_FALSE;
neti_stack_t *nts;
nts = net_getnetistackbyid(netid);
ASSERT(nts != NULL);
if (info == NULL)
found = B_TRUE;
mutex_enter(&nts->nts_lock);
LIST_FOREACH(n, &nts->nts_netd_head, netd_list) {
if (found) {
/*
* We are only interested in finding protocols that
* are not in some sort of shutdown state. There is
* no need to check for netd_stack==NULL because
* that implies it is no longer on this list.
*/
if (n->netd_condemned == 0)
continue;
break;
}
if (n == info)
found = B_TRUE;
}
if (info != NULL)
(void) net_protocol_release(info);
if (n != NULL)
atomic_inc_32((uint_t *)&n->netd_refcnt);
mutex_exit(&nts->nts_lock);
return (n);
}
/*
* Public accessor functions
*/
int
net_getifname(net_handle_t info, phy_if_t nic, char *buffer,
const size_t buflen)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_getifname(info, nic, buffer, buflen));
}
int
net_getmtu(net_handle_t info, phy_if_t nic, lif_if_t ifdata)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_getmtu(info, nic, ifdata));
}
int
net_getpmtuenabled(net_handle_t info)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_getpmtuenabled(info));
}
int
net_getlifaddr(net_handle_t info, phy_if_t nic, lif_if_t ifdata,
int nelem, net_ifaddr_t type[], void *storage)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_getlifaddr(info, nic, ifdata,
nelem, type, storage));
}
int
net_getlifzone(net_handle_t info, phy_if_t phy_ifdata, lif_if_t ifdata,
zoneid_t *zoneid)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.neti_getlifzone(info, phy_ifdata, ifdata,
zoneid));
}
int
net_getlifflags(net_handle_t info, phy_if_t phy_ifdata, lif_if_t ifdata,
uint64_t *flags)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.neti_getlifflags(info, phy_ifdata, ifdata,
flags));
}
phy_if_t
net_phygetnext(net_handle_t info, phy_if_t nic)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return ((phy_if_t)-1);
return (info->netd_info.netp_phygetnext(info, nic));
}
phy_if_t
net_phylookup(net_handle_t info, const char *name)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return ((phy_if_t)-1);
return (info->netd_info.netp_phylookup(info, name));
}
lif_if_t
net_lifgetnext(net_handle_t info, phy_if_t ifidx, lif_if_t ifdata)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return ((lif_if_t)-1);
return (info->netd_info.netp_lifgetnext(info, ifidx, ifdata));
}
int
net_inject(net_handle_t info, inject_t style, net_inject_t *packet)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_inject(info, style, packet));
}
phy_if_t
net_routeto(net_handle_t info, struct sockaddr *address, struct sockaddr *next)
{
ASSERT(info != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return ((phy_if_t)-1);
return (info->netd_info.netp_routeto(info, address, next));
}
int
net_ispartialchecksum(net_handle_t info, mblk_t *mp)
{
ASSERT(info != NULL);
ASSERT(mp != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_ispartialchecksum(info, mp));
}
int
net_isvalidchecksum(net_handle_t info, mblk_t *mp)
{
ASSERT(info != NULL);
ASSERT(mp != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (-1);
return (info->netd_info.netp_isvalidchecksum(info, mp));
}
/*
* Hooks related functions
*/
/*
* Function: net_family_register
* Returns: int - 0 = Succ, Else = Fail
* Parameters: info(I) - protocol
* hf(I) - family pointer
*
* Call hook_family_add to register family
*
* There is no need to bump netd_refcnt in the two functions
* net_family_register and net_family_unregister because the caller of these
* two functions is assumed to "own" a reference on 'info' via an earlier
* call to net_protocol_register(). Thus the owner is expected to do a
* call to net_protocol_unregister() after having done a
* net_family_unregister() to make sure things are properly cleaned up.
* Passing a pointer to info->netd_hooks into hook_family_add is required
* so that this can be set before the notify functions are called. If this
* does not happen, the notify function may do something that seems fine,
* like add a notify function to the family but cause a panic because
* netd_hooks is NULL when we get to hook_family_notify_register.
*/
int
net_family_register(net_handle_t info, hook_family_t *hf)
{
netstack_t *ns;
ASSERT(info != NULL);
ASSERT(hf != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (ESHUTDOWN);
if (info->netd_hooks != NULL)
return (EEXIST);
ns = info->netd_stack->nts_netstack;
ASSERT(ns != NULL);
if (hook_family_add(hf, ns->netstack_hook,
(void **)&info->netd_hooks) == NULL)
return (EEXIST);
return (0);
}
/*
* Function: net_family_unregister
* Returns: int - transparent value, explained by caller
* Parameters: info(I) - protocol
* hf(I) - family pointer
*
* Call hook_family_remove to unregister family
*/
int
net_family_unregister(net_handle_t info, hook_family_t *hf)
{
int ret;
ASSERT(info != NULL);
ASSERT(hf != NULL);
if (info->netd_hooks == NULL)
return (ENXIO);
if (strcmp(info->netd_hooks->hfi_family.hf_name,
hf->hf_name) != 0)
return (EINVAL);
ret = hook_family_remove(info->netd_hooks);
if (ret == 0)
info->netd_hooks = NULL;
return (ret);
}
int
net_family_shutdown(net_handle_t info, hook_family_t *hf)
{
ASSERT(info != NULL);
ASSERT(hf != NULL);
if (info->netd_hooks == NULL)
return (ENXIO);
if (strcmp(info->netd_hooks->hfi_family.hf_name,
hf->hf_name) != 0)
return (EINVAL);
return (hook_family_shutdown(info->netd_hooks));
}
/*
* Function: net_event_register
* Returns: internal event pointer - NULL = Fail
* Parameters: info(I) - protocol
* he(I) - event pointer
*
* Call hook_event_add to register event on specific family
* Internal event pointer is returned so caller can get
* handle to run hooks
*/
hook_event_token_t
net_event_register(net_handle_t info, hook_event_t *he)
{
hook_event_int_t *hei;
ASSERT(info != NULL);
ASSERT(he != NULL);
if (info->netd_hooks == NULL || info->netd_condemned != 0 ||
info->netd_stack == NULL)
return (NULL);
hei = hook_event_add(info->netd_hooks, he);
return ((hook_event_token_t)hei);
}
/*
* Function: net_event_unregister
* Returns: int - transparent value, explained by caller
* Parameters: info(I) - protocol
* he(I) - event pointer
*
* Call hook_event_remove to unregister event on specific family
*/
int
net_event_unregister(net_handle_t info, hook_event_t *he)
{
ASSERT(info != NULL);
ASSERT(he != NULL);
if (info->netd_hooks == NULL)
return (ENXIO);
return (hook_event_remove(info->netd_hooks, he));
}
int
net_event_shutdown(net_handle_t info, hook_event_t *he)
{
ASSERT(info != NULL);
ASSERT(he != NULL);
if (info->netd_hooks == NULL)
return (ENXIO);
return (hook_event_shutdown(info->netd_hooks, he));
}
/*
* Function: net_hook_register
* Returns: int - transparent value, explained by caller
* Parameters: info(I) - protocol
* event(I) - event name
* h(I) - hook pointer
*
* Call hook_register to add hook on specific family/event
*/
int
net_hook_register(net_handle_t info, char *event, hook_t *h)
{
ASSERT(info != NULL);
ASSERT(event != NULL);
ASSERT(h != NULL);
if (info->netd_condemned != 0 || info->netd_stack == NULL)
return (ESHUTDOWN);
if (info->netd_hooks == NULL)
return (ENXIO);
return (hook_register(info->netd_hooks, event, h));
}
/*
* Function: net_hook_unregister
* Returns: int - transparent value, explained by caller
* Parameters: info(I) - protocol
* event(I) - event name
* h(I) - hook pointer
*
* Call hook_unregister to remove hook on specific family/event
*/
int
net_hook_unregister(net_handle_t info, char *event, hook_t *h)
{
ASSERT(info != NULL);
ASSERT(event != NULL);
ASSERT(h != NULL);
if (info->netd_hooks == NULL)
return (ENXIO);
return (hook_unregister(info->netd_hooks, event, h));
}
netid_t
net_getnetid(net_handle_t netd)
{
if (netd->netd_stack == NULL)
return (-1);
return (netd->netd_stack->nts_id);
}
net_inject_t *
net_inject_alloc(const int version)
{
net_inject_t *ni;
ni = kmem_zalloc(sizeof (*ni), KM_NOSLEEP);
if (ni == NULL)
return (NULL);
ni->ni_version = version;
return (ni);
}
void
net_inject_free(net_inject_t *ni)
{
kmem_free(ni, sizeof (*ni));
}
kstat_t *
net_kstat_create(netid_t netid, char *module, int instance, char *name,
char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
{
netstackid_t stackid = net_getnetstackidbynetid(netid);
if (stackid == -1)
return (NULL);
return (kstat_create_netstack(module, instance, name, class, type,
ndata, ks_flag, stackid));
}
void
net_kstat_delete(netid_t netid, kstat_t *ks)
{
netstackid_t stackid = net_getnetstackidbynetid(netid);
if (stackid != -1)
kstat_delete_netstack(ks, stackid);
}
int
net_event_notify_register(net_handle_t family, char *event,
hook_notify_fn_t callback, void *arg)
{
int error;
if (family->netd_condemned != 0 || family->netd_stack == NULL)
return (ESHUTDOWN);
error = hook_event_notify_register(family->netd_hooks, event,
callback, arg);
return (error);
}
int
net_event_notify_unregister(net_handle_t family, char *event,
hook_notify_fn_t callback)
{
int error;
error = hook_event_notify_unregister(family->netd_hooks, event,
callback);
return (error);
}
int
net_protocol_notify_register(net_handle_t family, hook_notify_fn_t callback,
void *arg)
{
int error;
if (family->netd_condemned != 0 || family->netd_stack == NULL)
return (ESHUTDOWN);
error = hook_family_notify_register(family->netd_hooks, callback,
arg);
return (error);
}
int
net_protocol_notify_unregister(net_handle_t family, hook_notify_fn_t callback)
{
int error;
error = hook_family_notify_unregister(family->netd_hooks, callback);
return (error);
}
|