npfext_normalize.c revision 1.2
11.2Srmind/*	$NetBSD: npfext_normalize.c,v 1.2 2018/09/29 14:41:37 rmind Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 2012 The NetBSD Foundation, Inc.
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * Redistribution and use in source and binary forms, with or without
81.1Schristos * modification, are permitted provided that the following conditions
91.1Schristos * are met:
101.1Schristos * 1. Redistributions of source code must retain the above copyright
111.1Schristos *    notice, this list of conditions and the following disclaimer.
121.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
131.1Schristos *    notice, this list of conditions and the following disclaimer in the
141.1Schristos *    documentation and/or other materials provided with the distribution.
151.1Schristos *
161.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Schristos * POSSIBILITY OF SUCH DAMAGE.
271.1Schristos */
281.1Schristos
291.1Schristos#include <sys/cdefs.h>
301.2Srmind__RCSID("$NetBSD: npfext_normalize.c,v 1.2 2018/09/29 14:41:37 rmind Exp $");
311.1Schristos
321.1Schristos#include <stdlib.h>
331.2Srmind#include <stdbool.h>
341.1Schristos#include <string.h>
351.1Schristos#include <assert.h>
361.1Schristos#include <errno.h>
371.1Schristos
381.1Schristos#include <npf.h>
391.1Schristos
401.1Schristosint		npfext_normalize_init(void);
411.1Schristosnl_ext_t *	npfext_normalize_construct(const char *);
421.1Schristosint		npfext_normalize_param(nl_ext_t *, const char *, const char *);
431.1Schristos
441.1Schristosint
451.1Schristosnpfext_normalize_init(void)
461.1Schristos{
471.1Schristos	/* Nothing to initialize. */
481.1Schristos	return 0;
491.1Schristos}
501.1Schristos
511.1Schristosnl_ext_t *
521.1Schristosnpfext_normalize_construct(const char *name)
531.1Schristos{
541.1Schristos	assert(strcmp(name, "normalize") == 0);
551.1Schristos	return npf_ext_construct(name);
561.1Schristos}
571.1Schristos
581.1Schristosint
591.1Schristosnpfext_normalize_param(nl_ext_t *ext, const char *param, const char *val)
601.1Schristos{
611.1Schristos	enum ptype {
621.1Schristos		PARAM_BOOL,
631.1Schristos		PARAM_U32
641.1Schristos	};
651.1Schristos	static const struct param {
661.1Schristos		const char *	name;
671.1Schristos		enum ptype	type;
681.1Schristos		bool		reqval;
691.1Schristos	} params[] = {
701.1Schristos		{ "random-id",	PARAM_BOOL,	false	},
711.1Schristos		{ "no-df",	PARAM_BOOL,	false	},
721.1Schristos		{ "min-ttl",	PARAM_U32,	true	},
731.1Schristos		{ "max-mss",	PARAM_U32,	true	},
741.1Schristos	};
751.1Schristos
761.1Schristos	for (unsigned i = 0; i < __arraycount(params); i++) {
771.1Schristos		const char *name = params[i].name;
781.1Schristos
791.1Schristos		if (strcmp(name, param) != 0) {
801.1Schristos			continue;
811.1Schristos		}
821.1Schristos		if (val == NULL && params[i].reqval) {
831.1Schristos			return EINVAL;
841.1Schristos		}
851.1Schristos
861.1Schristos		switch (params[i].type) {
871.1Schristos		case PARAM_BOOL:
881.1Schristos			npf_ext_param_bool(ext, name, true);
891.1Schristos			break;
901.1Schristos		case PARAM_U32:
911.1Schristos			npf_ext_param_u32(ext, name, atol(val));
921.1Schristos			break;
931.1Schristos		default:
941.1Schristos			assert(false);
951.1Schristos		}
961.1Schristos		return 0;
971.1Schristos	}
981.1Schristos
991.1Schristos	/* Invalid parameter, if not found. */
1001.1Schristos	return EINVAL;
1011.1Schristos}
102