1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright (c) 2012, OmniTI Computer Consulting, Inc. All rights reserved. 14 */ 15 16 /* 17 * This file implements a socketfilter used to deter TCP connections. 18 * To defer a connection means to delay the return of accept(3SOCKET) 19 * until at least one byte is ready to be read(2). This filter may be 20 * applied automatically or programmatically through the use of 21 * soconfig(1M) and setsockopt(3SOCKET). 22 */ 23 24 #include <sys/kmem.h> 25 #include <sys/systm.h> 26 #include <sys/stropts.h> 27 #include <sys/strsun.h> 28 #include <sys/socketvar.h> 29 #include <sys/sockfilter.h> 30 #include <sys/note.h> 31 #include <sys/taskq.h> 32 33 #define DATAFILT_MODULE "datafilt" 34 35 static struct modlmisc dataf_modlmisc = { 36 &mod_miscops, 37 "Kernel data-ready socket filter" 38 }; 39 40 static struct modlinkage dataf_modlinkage = { 41 MODREV_1, 42 &dataf_modlmisc, 43 NULL 44 }; 45 46 static sof_rval_t 47 dataf_attach_passive_cb(sof_handle_t handle, sof_handle_t ph, 48 void *parg, struct sockaddr *laddr, socklen_t laddrlen, 49 struct sockaddr *faddr, socklen_t faddrlen, void **cookiep) 50 { 51 _NOTE(ARGUNUSED(handle, ph, parg, laddr, laddrlen, faddr, faddrlen, 52 cookiep)); 53 return (SOF_RVAL_DEFER); 54 } 55 56 static void 57 dataf_detach_cb(sof_handle_t handle, void *cookie, cred_t *cr) 58 { 59 _NOTE(ARGUNUSED(handle, cookie, cr)); 60 } 61 62 static mblk_t * 63 dataf_data_in_cb(sof_handle_t handle, void *cookie, mblk_t *mp, int flags, 64 size_t *lenp) 65 { 66 _NOTE(ARGUNUSED(cookie, flags, lenp)); 67 68 if (mp != NULL && MBLKL(mp) > 0) { 69 sof_newconn_ready(handle); 70 sof_bypass(handle); 71 } 72 73 return (mp); 74 } 75 76 static sof_ops_t dataf_ops = { 77 .sofop_attach_passive = dataf_attach_passive_cb, 78 .sofop_detach = dataf_detach_cb, 79 .sofop_data_in = dataf_data_in_cb 80 }; 81 82 int 83 _init(void) 84 { 85 int err; 86 87 /* 88 * This module is safe to attach even after some preliminary socket 89 * setup calls have taken place. See the comment for SOF_ATT_SAFE. 90 */ 91 err = sof_register(SOF_VERSION, DATAFILT_MODULE, &dataf_ops, 92 SOF_ATT_SAFE); 93 if (err != 0) 94 return (err); 95 if ((err = mod_install(&dataf_modlinkage)) != 0) 96 (void) sof_unregister(DATAFILT_MODULE); 97 98 return (err); 99 } 100 101 int 102 _fini(void) 103 { 104 int err; 105 106 if ((err = sof_unregister(DATAFILT_MODULE)) != 0) 107 return (err); 108 109 return (mod_remove(&dataf_modlinkage)); 110 } 111 112 int 113 _info(struct modinfo *modinfop) 114 { 115 return (mod_info(&dataf_modlinkage, modinfop)); 116 }  | 1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2012, OmniTI Computer Consulting, Inc. All rights reserved. 23 */ 24 25 #include <sys/kmem.h> 26 #include <sys/systm.h> 27 #include <sys/stropts.h> 28 #include <sys/strsun.h> 29 #include <sys/socketvar.h> 30 #include <sys/sockfilter.h> 31 #include <sys/note.h> 32 #include <sys/taskq.h> 33 34 static struct modlmisc dataf_modlmisc = { 35 &mod_miscops, 36 "Kernel data-ready socket filter" 37 }; 38 39 static struct modlinkage dataf_modlinkage = { 40 MODREV_1, 41 &dataf_modlmisc, 42 NULL 43 }; 44 45 #define DATAFILT_MODULE "datafilt" 46 47 /* ARGSUSED */ 48 sof_rval_t 49 dataf_attach_passive_cb(sof_handle_t handle, sof_handle_t ph, 50 void *parg, struct sockaddr *laddr, socklen_t laddrlen, 51 struct sockaddr *faddr, socklen_t faddrlen, void **cookiep) 52 { 53 return (SOF_RVAL_DEFER); 54 } 55 56 void 57 dataf_detach_cb(sof_handle_t handle, void *cookie, cred_t *cr) 58 { 59 _NOTE(ARGUNUSED(handle, cookie, cr)); 60 } 61 62 /* 63 * Called for each incoming segment. 64 */ 65 mblk_t * 66 dataf_data_in_cb(sof_handle_t handle, void *cookie, mblk_t *mp, int flags, 67 size_t *lenp) 68 { 69 _NOTE(ARGUNUSED(cookie, flags, lenp)); 70 71 if (mp != NULL && MBLKL(mp) > 0) 72 sof_newconn_ready(handle); 73 74 return (mp); 75 } 76 77 sof_ops_t dataf_ops = { 78 .sofop_attach_passive = dataf_attach_passive_cb, 79 .sofop_detach = dataf_detach_cb, 80 .sofop_data_in = dataf_data_in_cb 81 }; 82 83 int 84 _init(void) 85 { 86 int error; 87 88 /* 89 * This module is safe to attach even after some preliminary socket 90 * setup calls have taken place. See the comment for SOF_ATT_SAFE. 91 */ 92 error = sof_register(SOF_VERSION, DATAFILT_MODULE, &dataf_ops, 93 SOF_ATT_SAFE); 94 if (error != 0) 95 return (error); 96 if ((error = mod_install(&dataf_modlinkage)) != 0) 97 (void) sof_unregister(DATAFILT_MODULE); 98 99 return (error); 100 } 101 102 int 103 _fini(void) 104 { 105 int error; 106 107 if ((error = sof_unregister(DATAFILT_MODULE)) != 0) 108 return (error); 109 110 return (mod_remove(&dataf_modlinkage)); 111 } 112 113 int 114 _info(struct modinfo *modinfop) 115 { 116 return (mod_info(&dataf_modlinkage, modinfop)); 117 }  |