Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ipfou.c FOU (foo over UDP) support |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Tom Herbert <therbert@google.com> |
| 10 | */ |
| 11 | |
| 12 | #include <netdb.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <net/if.h> |
| 17 | #include <linux/fou.h> |
| 18 | #include <linux/genetlink.h> |
| 19 | #include <linux/ip.h> |
| 20 | #include <arpa/inet.h> |
| 21 | |
| 22 | #include "libgenl.h" |
| 23 | #include "utils.h" |
| 24 | #include "ip_common.h" |
| 25 | |
| 26 | static void usage(void) |
| 27 | { |
Tom Herbert | 8bd31d8 | 2016-08-04 13:34:57 -0700 | [diff] [blame] | 28 | fprintf(stderr, "Usage: ip fou add port PORT " |
| 29 | "{ ipproto PROTO | gue } [ -6 ]\n"); |
| 30 | fprintf(stderr, " ip fou del port PORT [ -6 ]\n"); |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 31 | fprintf(stderr, "\n"); |
| 32 | fprintf(stderr, "Where: PROTO { ipproto-name | 1..255 }\n"); |
| 33 | fprintf(stderr, " PORT { 1..65535 }\n"); |
| 34 | |
| 35 | exit(-1); |
| 36 | } |
| 37 | |
| 38 | /* netlink socket */ |
| 39 | static struct rtnl_handle genl_rth = { .fd = -1 }; |
| 40 | static int genl_family = -1; |
| 41 | |
| 42 | #define FOU_REQUEST(_req, _bufsiz, _cmd, _flags) \ |
| 43 | GENL_REQUEST(_req, _bufsiz, genl_family, 0, \ |
| 44 | FOU_GENL_VERSION, _cmd, _flags) |
| 45 | |
| 46 | static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n, |
| 47 | bool adding) |
| 48 | { |
| 49 | __u16 port; |
| 50 | int port_set = 0; |
| 51 | __u8 ipproto, type; |
| 52 | bool gue_set = false; |
| 53 | int ipproto_set = 0; |
Tom Herbert | 8bd31d8 | 2016-08-04 13:34:57 -0700 | [diff] [blame] | 54 | unsigned short family = AF_INET; |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 55 | |
| 56 | while (argc > 0) { |
| 57 | if (!matches(*argv, "port")) { |
| 58 | NEXT_ARG(); |
| 59 | |
Sabrina Dubroca | 9f7401f | 2016-06-03 16:45:46 +0200 | [diff] [blame] | 60 | if (get_be16(&port, *argv, 0) || port == 0) |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 61 | invarg("invalid port", *argv); |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 62 | port_set = 1; |
| 63 | } else if (!matches(*argv, "ipproto")) { |
| 64 | struct protoent *servptr; |
| 65 | |
| 66 | NEXT_ARG(); |
| 67 | |
| 68 | servptr = getprotobyname(*argv); |
| 69 | if (servptr) |
| 70 | ipproto = servptr->p_proto; |
| 71 | else if (get_u8(&ipproto, *argv, 0) || ipproto == 0) |
| 72 | invarg("invalid ipproto", *argv); |
| 73 | ipproto_set = 1; |
| 74 | } else if (!matches(*argv, "gue")) { |
| 75 | gue_set = true; |
Tom Herbert | 8bd31d8 | 2016-08-04 13:34:57 -0700 | [diff] [blame] | 76 | } else if (!matches(*argv, "-6")) { |
| 77 | family = AF_INET6; |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 78 | } else { |
| 79 | fprintf(stderr, "fou: unknown command \"%s\"?\n", *argv); |
| 80 | usage(); |
| 81 | return -1; |
| 82 | } |
| 83 | argc--, argv++; |
| 84 | } |
| 85 | |
| 86 | if (!port_set) { |
| 87 | fprintf(stderr, "fou: missing port\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | if (!ipproto_set && !gue_set && adding) { |
| 92 | fprintf(stderr, "fou: must set ipproto or gue\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | if (ipproto_set && gue_set) { |
| 97 | fprintf(stderr, "fou: cannot set ipproto and gue\n"); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | type = gue_set ? FOU_ENCAP_GUE : FOU_ENCAP_DIRECT; |
| 102 | |
| 103 | addattr16(n, 1024, FOU_ATTR_PORT, port); |
| 104 | addattr8(n, 1024, FOU_ATTR_TYPE, type); |
Tom Herbert | 8bd31d8 | 2016-08-04 13:34:57 -0700 | [diff] [blame] | 105 | addattr16(n, 1024, FOU_ATTR_AF, family); |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 106 | |
| 107 | if (ipproto_set) |
| 108 | addattr8(n, 1024, FOU_ATTR_IPPROTO, ipproto); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int do_add(int argc, char **argv) |
| 114 | { |
| 115 | FOU_REQUEST(req, 1024, FOU_CMD_ADD, NLM_F_REQUEST); |
| 116 | |
| 117 | fou_parse_opt(argc, argv, &req.n, true); |
| 118 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 119 | if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0) |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 120 | return -2; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int do_del(int argc, char **argv) |
| 126 | { |
| 127 | FOU_REQUEST(req, 1024, FOU_CMD_DEL, NLM_F_REQUEST); |
| 128 | |
| 129 | fou_parse_opt(argc, argv, &req.n, false); |
| 130 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 131 | if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0) |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 132 | return -2; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int do_ipfou(int argc, char **argv) |
| 138 | { |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 139 | if (argc < 1) |
| 140 | usage(); |
| 141 | |
Sabrina Dubroca | d240a0e | 2016-08-16 16:26:57 +0200 | [diff] [blame^] | 142 | if (matches(*argv, "help") == 0) |
| 143 | usage(); |
| 144 | |
| 145 | if (genl_init_handle(&genl_rth, FOU_GENL_NAME, &genl_family)) |
| 146 | exit(1); |
| 147 | |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 148 | if (matches(*argv, "add") == 0) |
| 149 | return do_add(argc-1, argv+1); |
| 150 | if (matches(*argv, "delete") == 0) |
| 151 | return do_del(argc-1, argv+1); |
Tom Herbert | 6928747 | 2014-11-05 10:06:24 -0800 | [diff] [blame] | 152 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip fou help\".\n", *argv); |
| 153 | exit(-1); |
| 154 | } |