xref: /freebsd/sys/netgraph/ng_ip_input.c (revision b8a6e03fac922677455d8e0977831506cf8212e8)
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  * $FreeBSD$
70c2eed105SBrooks Davis  */
71c2eed105SBrooks Davis 
72c2eed105SBrooks Davis /*
73c2eed105SBrooks Davis  * This node simply takes IP packets and calls ip_input on them
74c2eed105SBrooks Davis  */
75c2eed105SBrooks Davis 
76c2eed105SBrooks Davis #include <sys/param.h>
77c2eed105SBrooks Davis #include <sys/systm.h>
78c2eed105SBrooks Davis #include <sys/kernel.h>
79c2eed105SBrooks Davis #include <sys/malloc.h>
80c2eed105SBrooks Davis #include <sys/mbuf.h>
81c2eed105SBrooks Davis #include <sys/socket.h>
82f089869fSMarko Zec #include <sys/proc.h>
83c2eed105SBrooks Davis #include <net/if.h>
84c2eed105SBrooks Davis #include <net/if_types.h>
85c2eed105SBrooks Davis #include <net/if_var.h>
86c2eed105SBrooks Davis #include <netinet/in.h>
87c2eed105SBrooks Davis #include <netinet/in_var.h>
88c2eed105SBrooks Davis #include <net/netisr.h>
89c2eed105SBrooks Davis #include <netgraph/ng_message.h>
90c2eed105SBrooks Davis #include <netgraph/netgraph.h>
91c2eed105SBrooks Davis #include <netgraph/ng_ip_input.h>
92c2eed105SBrooks Davis 
93c2eed105SBrooks Davis /* Netgraph methods */
94c2eed105SBrooks Davis static ng_constructor_t	ngipi_cons;
95c2eed105SBrooks Davis static ng_rcvdata_t	ngipi_rcvdata;
96c2eed105SBrooks Davis static ng_disconnect_t	ngipi_disconnect;
97c2eed105SBrooks Davis 
98c2eed105SBrooks Davis static struct ng_type typestruct = {
99f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
100f8aae777SJulian Elischer 	.name =		NG_IP_INPUT_NODE_TYPE,
101f8aae777SJulian Elischer 	.constructor =	ngipi_cons,
102f8aae777SJulian Elischer 	.rcvdata =	ngipi_rcvdata,
103f8aae777SJulian Elischer 	.disconnect =	ngipi_disconnect,
104c2eed105SBrooks Davis };
105c2eed105SBrooks Davis NETGRAPH_INIT(ip_input, &typestruct);
106c2eed105SBrooks Davis 
107c2eed105SBrooks Davis /*
108c2eed105SBrooks Davis  * Be obliging. but no work to do.
109c2eed105SBrooks Davis  */
110c2eed105SBrooks Davis static int
111c2eed105SBrooks Davis ngipi_cons(node_p node)
112c2eed105SBrooks Davis {
113c2eed105SBrooks Davis 	return(0);
114c2eed105SBrooks Davis }
115c2eed105SBrooks Davis 
116c2eed105SBrooks Davis /*
117c2eed105SBrooks Davis  * Receive data
118c2eed105SBrooks Davis  */
119c2eed105SBrooks Davis static int
120c2eed105SBrooks Davis ngipi_rcvdata(hook_p hook, item_p item)
121c2eed105SBrooks Davis {
122c2eed105SBrooks Davis 	struct mbuf *m;
123c2eed105SBrooks Davis 
124c2eed105SBrooks Davis 	NGI_GET_M(item, m);
125c2eed105SBrooks Davis 	NG_FREE_ITEM(item);
126f089869fSMarko Zec 	if (curthread->td_ng_outbound)
127f089869fSMarko Zec 		netisr_queue(NETISR_IP, m);
128*b8a6e03fSGleb Smirnoff 	else {
129*b8a6e03fSGleb Smirnoff 		struct epoch_tracker et;
130*b8a6e03fSGleb Smirnoff 
131*b8a6e03fSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1321566e059SOleksandr Tymoshenko 		netisr_dispatch(NETISR_IP, m);
133*b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
134*b8a6e03fSGleb Smirnoff 	}
135c2eed105SBrooks Davis 	return 0;
136c2eed105SBrooks Davis }
137c2eed105SBrooks Davis 
138c2eed105SBrooks Davis /*
139c2eed105SBrooks Davis  * Hook disconnection
140c2eed105SBrooks Davis  */
141c2eed105SBrooks Davis static int
142c2eed105SBrooks Davis ngipi_disconnect(hook_p hook)
143c2eed105SBrooks Davis {
144c2eed105SBrooks Davis 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
145c2eed105SBrooks Davis 		ng_rmnode_self(NG_HOOK_NODE(hook));
146c2eed105SBrooks Davis 	return (0);
147c2eed105SBrooks Davis }
148