xref: /freebsd/sys/netgraph/ng_ip_input.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * ng_ip_input.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause
7  *
8  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1.  Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions, and the following disclaimer.
16  * 2.  Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions, and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  * 3.  The name of The Aerospace Corporation may not be used to endorse or
20  *     promote products derived from this software.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Copyright (c) 1996-1999 Whistle Communications, Inc.
35  * All rights reserved.
36  *
37  * Subject to the following obligations and disclaimer of warranty, use and
38  * redistribution of this software, in source or object code forms, with or
39  * without modifications are expressly permitted by Whistle Communications;
40  * provided, however, that:
41  * 1. Any and all reproductions of the source or object code must include the
42  *    copyright notice above and the following disclaimer of warranties; and
43  * 2. No rights are granted, in any manner or form, to use Whistle
44  *    Communications, Inc. trademarks, including the mark "WHISTLE
45  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
46  *    such appears in the above copyright notice or in the software.
47  *
48  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
49  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
50  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
51  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
52  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
53  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
54  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
55  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
56  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
57  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
58  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
59  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
60  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
64  * OF SUCH DAMAGE.
65  *
66  * Author:		Brooks Davis <brooks@FreeBSD.org>
67  * Derived from:	ng_hole.c
68  *
69  * $FreeBSD$
70  */
71 
72 /*
73  * This node simply takes IP packets and calls ip_input on them
74  */
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/socket.h>
82 #include <sys/proc.h>
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/if_var.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <net/netisr.h>
89 #include <netgraph/ng_message.h>
90 #include <netgraph/netgraph.h>
91 #include <netgraph/ng_ip_input.h>
92 
93 /* Netgraph methods */
94 static ng_constructor_t	ngipi_cons;
95 static ng_rcvdata_t	ngipi_rcvdata;
96 static ng_disconnect_t	ngipi_disconnect;
97 
98 static struct ng_type typestruct = {
99 	.version =	NG_ABI_VERSION,
100 	.name =		NG_IP_INPUT_NODE_TYPE,
101 	.constructor =	ngipi_cons,
102 	.rcvdata =	ngipi_rcvdata,
103 	.disconnect =	ngipi_disconnect,
104 };
105 NETGRAPH_INIT(ip_input, &typestruct);
106 
107 /*
108  * Be obliging. but no work to do.
109  */
110 static int
111 ngipi_cons(node_p node)
112 {
113 	return(0);
114 }
115 
116 /*
117  * Receive data
118  */
119 static int
120 ngipi_rcvdata(hook_p hook, item_p item)
121 {
122 	struct mbuf *m;
123 
124 	NGI_GET_M(item, m);
125 	NG_FREE_ITEM(item);
126 	if (curthread->td_ng_outbound)
127 		netisr_queue(NETISR_IP, m);
128 	else {
129 		struct epoch_tracker et;
130 
131 		NET_EPOCH_ENTER(et);
132 		netisr_dispatch(NETISR_IP, m);
133 		NET_EPOCH_EXIT(et);
134 	}
135 	return 0;
136 }
137 
138 /*
139  * Hook disconnection
140  */
141 static int
142 ngipi_disconnect(hook_p hook)
143 {
144 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
145 		ng_rmnode_self(NG_HOOK_NODE(hook));
146 	return (0);
147 }
148