1*39013e66Srmind /* $NetBSD: npfext_normalize.c,v 1.2 2018/09/29 14:41:37 rmind Exp $ */
22c148c82Schristos
32c148c82Schristos /*-
42c148c82Schristos * Copyright (c) 2012 The NetBSD Foundation, Inc.
52c148c82Schristos * All rights reserved.
62c148c82Schristos *
72c148c82Schristos * Redistribution and use in source and binary forms, with or without
82c148c82Schristos * modification, are permitted provided that the following conditions
92c148c82Schristos * are met:
102c148c82Schristos * 1. Redistributions of source code must retain the above copyright
112c148c82Schristos * notice, this list of conditions and the following disclaimer.
122c148c82Schristos * 2. Redistributions in binary form must reproduce the above copyright
132c148c82Schristos * notice, this list of conditions and the following disclaimer in the
142c148c82Schristos * documentation and/or other materials provided with the distribution.
152c148c82Schristos *
162c148c82Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172c148c82Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182c148c82Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192c148c82Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202c148c82Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212c148c82Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222c148c82Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232c148c82Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242c148c82Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252c148c82Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262c148c82Schristos * POSSIBILITY OF SUCH DAMAGE.
272c148c82Schristos */
282c148c82Schristos
292c148c82Schristos #include <sys/cdefs.h>
30*39013e66Srmind __RCSID("$NetBSD: npfext_normalize.c,v 1.2 2018/09/29 14:41:37 rmind Exp $");
312c148c82Schristos
322c148c82Schristos #include <stdlib.h>
33*39013e66Srmind #include <stdbool.h>
342c148c82Schristos #include <string.h>
352c148c82Schristos #include <assert.h>
362c148c82Schristos #include <errno.h>
372c148c82Schristos
382c148c82Schristos #include <npf.h>
392c148c82Schristos
402c148c82Schristos int npfext_normalize_init(void);
412c148c82Schristos nl_ext_t * npfext_normalize_construct(const char *);
422c148c82Schristos int npfext_normalize_param(nl_ext_t *, const char *, const char *);
432c148c82Schristos
442c148c82Schristos int
npfext_normalize_init(void)452c148c82Schristos npfext_normalize_init(void)
462c148c82Schristos {
472c148c82Schristos /* Nothing to initialize. */
482c148c82Schristos return 0;
492c148c82Schristos }
502c148c82Schristos
512c148c82Schristos nl_ext_t *
npfext_normalize_construct(const char * name)522c148c82Schristos npfext_normalize_construct(const char *name)
532c148c82Schristos {
542c148c82Schristos assert(strcmp(name, "normalize") == 0);
552c148c82Schristos return npf_ext_construct(name);
562c148c82Schristos }
572c148c82Schristos
582c148c82Schristos int
npfext_normalize_param(nl_ext_t * ext,const char * param,const char * val)592c148c82Schristos npfext_normalize_param(nl_ext_t *ext, const char *param, const char *val)
602c148c82Schristos {
612c148c82Schristos enum ptype {
622c148c82Schristos PARAM_BOOL,
632c148c82Schristos PARAM_U32
642c148c82Schristos };
652c148c82Schristos static const struct param {
662c148c82Schristos const char * name;
672c148c82Schristos enum ptype type;
682c148c82Schristos bool reqval;
692c148c82Schristos } params[] = {
702c148c82Schristos { "random-id", PARAM_BOOL, false },
712c148c82Schristos { "no-df", PARAM_BOOL, false },
722c148c82Schristos { "min-ttl", PARAM_U32, true },
732c148c82Schristos { "max-mss", PARAM_U32, true },
742c148c82Schristos };
752c148c82Schristos
762c148c82Schristos for (unsigned i = 0; i < __arraycount(params); i++) {
772c148c82Schristos const char *name = params[i].name;
782c148c82Schristos
792c148c82Schristos if (strcmp(name, param) != 0) {
802c148c82Schristos continue;
812c148c82Schristos }
822c148c82Schristos if (val == NULL && params[i].reqval) {
832c148c82Schristos return EINVAL;
842c148c82Schristos }
852c148c82Schristos
862c148c82Schristos switch (params[i].type) {
872c148c82Schristos case PARAM_BOOL:
882c148c82Schristos npf_ext_param_bool(ext, name, true);
892c148c82Schristos break;
902c148c82Schristos case PARAM_U32:
912c148c82Schristos npf_ext_param_u32(ext, name, atol(val));
922c148c82Schristos break;
932c148c82Schristos default:
942c148c82Schristos assert(false);
952c148c82Schristos }
962c148c82Schristos return 0;
972c148c82Schristos }
982c148c82Schristos
992c148c82Schristos /* Invalid parameter, if not found. */
1002c148c82Schristos return EINVAL;
1012c148c82Schristos }
102