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