fou.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. #include <linux/module.h>
  2. #include <linux/errno.h>
  3. #include <linux/socket.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/ip.h>
  6. #include <linux/udp.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <net/genetlink.h>
  10. #include <net/gue.h>
  11. #include <net/ip.h>
  12. #include <net/protocol.h>
  13. #include <net/udp.h>
  14. #include <net/udp_tunnel.h>
  15. #include <net/xfrm.h>
  16. #include <uapi/linux/fou.h>
  17. #include <uapi/linux/genetlink.h>
  18. struct fou {
  19. struct socket *sock;
  20. u8 protocol;
  21. u8 flags;
  22. __be16 port;
  23. u16 type;
  24. struct udp_offload udp_offloads;
  25. struct list_head list;
  26. struct rcu_head rcu;
  27. };
  28. #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
  29. struct fou_cfg {
  30. u16 type;
  31. u8 protocol;
  32. u8 flags;
  33. struct udp_port_cfg udp_config;
  34. };
  35. static unsigned int fou_net_id;
  36. struct fou_net {
  37. struct list_head fou_list;
  38. struct mutex fou_lock;
  39. };
  40. static inline struct fou *fou_from_sock(struct sock *sk)
  41. {
  42. return sk->sk_user_data;
  43. }
  44. static int fou_recv_pull(struct sk_buff *skb, size_t len)
  45. {
  46. struct iphdr *iph = ip_hdr(skb);
  47. /* Remove 'len' bytes from the packet (UDP header and
  48. * FOU header if present).
  49. */
  50. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  51. __skb_pull(skb, len);
  52. skb_postpull_rcsum(skb, udp_hdr(skb), len);
  53. skb_reset_transport_header(skb);
  54. return iptunnel_pull_offloads(skb);
  55. }
  56. static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
  57. {
  58. struct fou *fou = fou_from_sock(sk);
  59. if (!fou)
  60. return 1;
  61. if (fou_recv_pull(skb, sizeof(struct udphdr)))
  62. goto drop;
  63. return -fou->protocol;
  64. drop:
  65. kfree_skb(skb);
  66. return 0;
  67. }
  68. static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
  69. void *data, size_t hdrlen, u8 ipproto,
  70. bool nopartial)
  71. {
  72. __be16 *pd = data;
  73. size_t start = ntohs(pd[0]);
  74. size_t offset = ntohs(pd[1]);
  75. size_t plen = sizeof(struct udphdr) + hdrlen +
  76. max_t(size_t, offset + sizeof(u16), start);
  77. if (skb->remcsum_offload)
  78. return guehdr;
  79. if (!pskb_may_pull(skb, plen))
  80. return NULL;
  81. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  82. skb_remcsum_process(skb, (void *)guehdr + hdrlen,
  83. start, offset, nopartial);
  84. return guehdr;
  85. }
  86. static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
  87. {
  88. /* No support yet */
  89. kfree_skb(skb);
  90. return 0;
  91. }
  92. static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
  93. {
  94. struct fou *fou = fou_from_sock(sk);
  95. size_t len, optlen, hdrlen;
  96. struct guehdr *guehdr;
  97. void *data;
  98. u16 doffset = 0;
  99. u8 proto_ctype;
  100. if (!fou)
  101. return 1;
  102. len = sizeof(struct udphdr) + sizeof(struct guehdr);
  103. if (!pskb_may_pull(skb, len))
  104. goto drop;
  105. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  106. optlen = guehdr->hlen << 2;
  107. len += optlen;
  108. if (!pskb_may_pull(skb, len))
  109. goto drop;
  110. /* guehdr may change after pull */
  111. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  112. hdrlen = sizeof(struct guehdr) + optlen;
  113. if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
  114. goto drop;
  115. hdrlen = sizeof(struct guehdr) + optlen;
  116. ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
  117. /* Pull csum through the guehdr now . This can be used if
  118. * there is a remote checksum offload.
  119. */
  120. skb_postpull_rcsum(skb, udp_hdr(skb), len);
  121. data = &guehdr[1];
  122. if (guehdr->flags & GUE_FLAG_PRIV) {
  123. __be32 flags = *(__be32 *)(data + doffset);
  124. doffset += GUE_LEN_PRIV;
  125. if (flags & GUE_PFLAG_REMCSUM) {
  126. guehdr = gue_remcsum(skb, guehdr, data + doffset,
  127. hdrlen, guehdr->proto_ctype,
  128. !!(fou->flags &
  129. FOU_F_REMCSUM_NOPARTIAL));
  130. if (!guehdr)
  131. goto drop;
  132. data = &guehdr[1];
  133. doffset += GUE_PLEN_REMCSUM;
  134. }
  135. }
  136. if (unlikely(guehdr->control))
  137. return gue_control_message(skb, guehdr);
  138. proto_ctype = guehdr->proto_ctype;
  139. __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
  140. skb_reset_transport_header(skb);
  141. if (iptunnel_pull_offloads(skb))
  142. goto drop;
  143. return -proto_ctype;
  144. drop:
  145. kfree_skb(skb);
  146. return 0;
  147. }
  148. static struct sk_buff **fou_gro_receive(struct sk_buff **head,
  149. struct sk_buff *skb,
  150. struct udp_offload *uoff)
  151. {
  152. const struct net_offload *ops;
  153. struct sk_buff **pp = NULL;
  154. u8 proto = NAPI_GRO_CB(skb)->proto;
  155. const struct net_offload **offloads;
  156. /* We can clear the encap_mark for FOU as we are essentially doing
  157. * one of two possible things. We are either adding an L4 tunnel
  158. * header to the outer L3 tunnel header, or we are are simply
  159. * treating the GRE tunnel header as though it is a UDP protocol
  160. * specific header such as VXLAN or GENEVE.
  161. */
  162. NAPI_GRO_CB(skb)->encap_mark = 0;
  163. rcu_read_lock();
  164. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  165. ops = rcu_dereference(offloads[proto]);
  166. if (!ops || !ops->callbacks.gro_receive)
  167. goto out_unlock;
  168. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  169. out_unlock:
  170. rcu_read_unlock();
  171. return pp;
  172. }
  173. static int fou_gro_complete(struct sk_buff *skb, int nhoff,
  174. struct udp_offload *uoff)
  175. {
  176. const struct net_offload *ops;
  177. u8 proto = NAPI_GRO_CB(skb)->proto;
  178. int err = -ENOSYS;
  179. const struct net_offload **offloads;
  180. udp_tunnel_gro_complete(skb, nhoff);
  181. rcu_read_lock();
  182. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  183. ops = rcu_dereference(offloads[proto]);
  184. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  185. goto out_unlock;
  186. err = ops->callbacks.gro_complete(skb, nhoff);
  187. out_unlock:
  188. rcu_read_unlock();
  189. return err;
  190. }
  191. static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
  192. struct guehdr *guehdr, void *data,
  193. size_t hdrlen, struct gro_remcsum *grc,
  194. bool nopartial)
  195. {
  196. __be16 *pd = data;
  197. size_t start = ntohs(pd[0]);
  198. size_t offset = ntohs(pd[1]);
  199. if (skb->remcsum_offload)
  200. return guehdr;
  201. if (!NAPI_GRO_CB(skb)->csum_valid)
  202. return NULL;
  203. guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
  204. start, offset, grc, nopartial);
  205. skb->remcsum_offload = 1;
  206. return guehdr;
  207. }
  208. static struct sk_buff **gue_gro_receive(struct sk_buff **head,
  209. struct sk_buff *skb,
  210. struct udp_offload *uoff)
  211. {
  212. const struct net_offload **offloads;
  213. const struct net_offload *ops;
  214. struct sk_buff **pp = NULL;
  215. struct sk_buff *p;
  216. struct guehdr *guehdr;
  217. size_t len, optlen, hdrlen, off;
  218. void *data;
  219. u16 doffset = 0;
  220. int flush = 1;
  221. struct fou *fou = container_of(uoff, struct fou, udp_offloads);
  222. struct gro_remcsum grc;
  223. skb_gro_remcsum_init(&grc);
  224. off = skb_gro_offset(skb);
  225. len = off + sizeof(*guehdr);
  226. guehdr = skb_gro_header_fast(skb, off);
  227. if (skb_gro_header_hard(skb, len)) {
  228. guehdr = skb_gro_header_slow(skb, len, off);
  229. if (unlikely(!guehdr))
  230. goto out;
  231. }
  232. optlen = guehdr->hlen << 2;
  233. len += optlen;
  234. if (skb_gro_header_hard(skb, len)) {
  235. guehdr = skb_gro_header_slow(skb, len, off);
  236. if (unlikely(!guehdr))
  237. goto out;
  238. }
  239. if (unlikely(guehdr->control) || guehdr->version != 0 ||
  240. validate_gue_flags(guehdr, optlen))
  241. goto out;
  242. hdrlen = sizeof(*guehdr) + optlen;
  243. /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
  244. * this is needed if there is a remote checkcsum offload.
  245. */
  246. skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
  247. data = &guehdr[1];
  248. if (guehdr->flags & GUE_FLAG_PRIV) {
  249. __be32 flags = *(__be32 *)(data + doffset);
  250. doffset += GUE_LEN_PRIV;
  251. if (flags & GUE_PFLAG_REMCSUM) {
  252. guehdr = gue_gro_remcsum(skb, off, guehdr,
  253. data + doffset, hdrlen, &grc,
  254. !!(fou->flags &
  255. FOU_F_REMCSUM_NOPARTIAL));
  256. if (!guehdr)
  257. goto out;
  258. data = &guehdr[1];
  259. doffset += GUE_PLEN_REMCSUM;
  260. }
  261. }
  262. skb_gro_pull(skb, hdrlen);
  263. flush = 0;
  264. for (p = *head; p; p = p->next) {
  265. const struct guehdr *guehdr2;
  266. if (!NAPI_GRO_CB(p)->same_flow)
  267. continue;
  268. guehdr2 = (struct guehdr *)(p->data + off);
  269. /* Compare base GUE header to be equal (covers
  270. * hlen, version, proto_ctype, and flags.
  271. */
  272. if (guehdr->word != guehdr2->word) {
  273. NAPI_GRO_CB(p)->same_flow = 0;
  274. continue;
  275. }
  276. /* Compare optional fields are the same. */
  277. if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
  278. guehdr->hlen << 2)) {
  279. NAPI_GRO_CB(p)->same_flow = 0;
  280. continue;
  281. }
  282. }
  283. /* We can clear the encap_mark for GUE as we are essentially doing
  284. * one of two possible things. We are either adding an L4 tunnel
  285. * header to the outer L3 tunnel header, or we are are simply
  286. * treating the GRE tunnel header as though it is a UDP protocol
  287. * specific header such as VXLAN or GENEVE.
  288. */
  289. NAPI_GRO_CB(skb)->encap_mark = 0;
  290. rcu_read_lock();
  291. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  292. ops = rcu_dereference(offloads[guehdr->proto_ctype]);
  293. if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
  294. goto out_unlock;
  295. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  296. out_unlock:
  297. rcu_read_unlock();
  298. out:
  299. NAPI_GRO_CB(skb)->flush |= flush;
  300. skb_gro_remcsum_cleanup(skb, &grc);
  301. return pp;
  302. }
  303. static int gue_gro_complete(struct sk_buff *skb, int nhoff,
  304. struct udp_offload *uoff)
  305. {
  306. const struct net_offload **offloads;
  307. struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
  308. const struct net_offload *ops;
  309. unsigned int guehlen;
  310. u8 proto;
  311. int err = -ENOENT;
  312. proto = guehdr->proto_ctype;
  313. guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
  314. rcu_read_lock();
  315. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  316. ops = rcu_dereference(offloads[proto]);
  317. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  318. goto out_unlock;
  319. err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
  320. out_unlock:
  321. rcu_read_unlock();
  322. return err;
  323. }
  324. static int fou_add_to_port_list(struct net *net, struct fou *fou)
  325. {
  326. struct fou_net *fn = net_generic(net, fou_net_id);
  327. struct fou *fout;
  328. mutex_lock(&fn->fou_lock);
  329. list_for_each_entry(fout, &fn->fou_list, list) {
  330. if (fou->port == fout->port) {
  331. mutex_unlock(&fn->fou_lock);
  332. return -EALREADY;
  333. }
  334. }
  335. list_add(&fou->list, &fn->fou_list);
  336. mutex_unlock(&fn->fou_lock);
  337. return 0;
  338. }
  339. static void fou_release(struct fou *fou)
  340. {
  341. struct socket *sock = fou->sock;
  342. struct sock *sk = sock->sk;
  343. if (sk->sk_family == AF_INET)
  344. udp_del_offload(&fou->udp_offloads);
  345. list_del(&fou->list);
  346. udp_tunnel_sock_release(sock);
  347. kfree_rcu(fou, rcu);
  348. }
  349. static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
  350. {
  351. udp_sk(sk)->encap_rcv = fou_udp_recv;
  352. fou->protocol = cfg->protocol;
  353. fou->udp_offloads.callbacks.gro_receive = fou_gro_receive;
  354. fou->udp_offloads.callbacks.gro_complete = fou_gro_complete;
  355. fou->udp_offloads.port = cfg->udp_config.local_udp_port;
  356. fou->udp_offloads.ipproto = cfg->protocol;
  357. return 0;
  358. }
  359. static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
  360. {
  361. udp_sk(sk)->encap_rcv = gue_udp_recv;
  362. fou->udp_offloads.callbacks.gro_receive = gue_gro_receive;
  363. fou->udp_offloads.callbacks.gro_complete = gue_gro_complete;
  364. fou->udp_offloads.port = cfg->udp_config.local_udp_port;
  365. return 0;
  366. }
  367. static int fou_create(struct net *net, struct fou_cfg *cfg,
  368. struct socket **sockp)
  369. {
  370. struct socket *sock = NULL;
  371. struct fou *fou = NULL;
  372. struct sock *sk;
  373. int err;
  374. /* Open UDP socket */
  375. err = udp_sock_create(net, &cfg->udp_config, &sock);
  376. if (err < 0)
  377. goto error;
  378. /* Allocate FOU port structure */
  379. fou = kzalloc(sizeof(*fou), GFP_KERNEL);
  380. if (!fou) {
  381. err = -ENOMEM;
  382. goto error;
  383. }
  384. sk = sock->sk;
  385. fou->flags = cfg->flags;
  386. fou->port = cfg->udp_config.local_udp_port;
  387. /* Initial for fou type */
  388. switch (cfg->type) {
  389. case FOU_ENCAP_DIRECT:
  390. err = fou_encap_init(sk, fou, cfg);
  391. if (err)
  392. goto error;
  393. break;
  394. case FOU_ENCAP_GUE:
  395. err = gue_encap_init(sk, fou, cfg);
  396. if (err)
  397. goto error;
  398. break;
  399. default:
  400. err = -EINVAL;
  401. goto error;
  402. }
  403. fou->type = cfg->type;
  404. udp_sk(sk)->encap_type = 1;
  405. udp_encap_enable();
  406. sk->sk_user_data = fou;
  407. fou->sock = sock;
  408. inet_inc_convert_csum(sk);
  409. sk->sk_allocation = GFP_ATOMIC;
  410. if (cfg->udp_config.family == AF_INET) {
  411. err = udp_add_offload(&fou->udp_offloads);
  412. if (err)
  413. goto error;
  414. }
  415. err = fou_add_to_port_list(net, fou);
  416. if (err)
  417. goto error;
  418. if (sockp)
  419. *sockp = sock;
  420. return 0;
  421. error:
  422. kfree(fou);
  423. if (sock)
  424. udp_tunnel_sock_release(sock);
  425. return err;
  426. }
  427. static int fou_destroy(struct net *net, struct fou_cfg *cfg)
  428. {
  429. struct fou_net *fn = net_generic(net, fou_net_id);
  430. __be16 port = cfg->udp_config.local_udp_port;
  431. int err = -EINVAL;
  432. struct fou *fou;
  433. mutex_lock(&fn->fou_lock);
  434. list_for_each_entry(fou, &fn->fou_list, list) {
  435. if (fou->port == port) {
  436. fou_release(fou);
  437. err = 0;
  438. break;
  439. }
  440. }
  441. mutex_unlock(&fn->fou_lock);
  442. return err;
  443. }
  444. static struct genl_family fou_nl_family = {
  445. .id = GENL_ID_GENERATE,
  446. .hdrsize = 0,
  447. .name = FOU_GENL_NAME,
  448. .version = FOU_GENL_VERSION,
  449. .maxattr = FOU_ATTR_MAX,
  450. .netnsok = true,
  451. };
  452. static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
  453. [FOU_ATTR_PORT] = { .type = NLA_U16, },
  454. [FOU_ATTR_AF] = { .type = NLA_U8, },
  455. [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
  456. [FOU_ATTR_TYPE] = { .type = NLA_U8, },
  457. [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
  458. };
  459. static int parse_nl_config(struct genl_info *info,
  460. struct fou_cfg *cfg)
  461. {
  462. memset(cfg, 0, sizeof(*cfg));
  463. cfg->udp_config.family = AF_INET;
  464. if (info->attrs[FOU_ATTR_AF]) {
  465. u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
  466. if (family != AF_INET)
  467. return -EINVAL;
  468. cfg->udp_config.family = family;
  469. }
  470. if (info->attrs[FOU_ATTR_PORT]) {
  471. __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
  472. cfg->udp_config.local_udp_port = port;
  473. }
  474. if (info->attrs[FOU_ATTR_IPPROTO])
  475. cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
  476. if (info->attrs[FOU_ATTR_TYPE])
  477. cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
  478. if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
  479. cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
  480. return 0;
  481. }
  482. static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
  483. {
  484. struct net *net = genl_info_net(info);
  485. struct fou_cfg cfg;
  486. int err;
  487. err = parse_nl_config(info, &cfg);
  488. if (err)
  489. return err;
  490. return fou_create(net, &cfg, NULL);
  491. }
  492. static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
  493. {
  494. struct net *net = genl_info_net(info);
  495. struct fou_cfg cfg;
  496. int err;
  497. err = parse_nl_config(info, &cfg);
  498. if (err)
  499. return err;
  500. return fou_destroy(net, &cfg);
  501. }
  502. static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
  503. {
  504. if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
  505. nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
  506. nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
  507. nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
  508. return -1;
  509. if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
  510. if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
  511. return -1;
  512. return 0;
  513. }
  514. static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
  515. u32 flags, struct sk_buff *skb, u8 cmd)
  516. {
  517. void *hdr;
  518. hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
  519. if (!hdr)
  520. return -ENOMEM;
  521. if (fou_fill_info(fou, skb) < 0)
  522. goto nla_put_failure;
  523. genlmsg_end(skb, hdr);
  524. return 0;
  525. nla_put_failure:
  526. genlmsg_cancel(skb, hdr);
  527. return -EMSGSIZE;
  528. }
  529. static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
  530. {
  531. struct net *net = genl_info_net(info);
  532. struct fou_net *fn = net_generic(net, fou_net_id);
  533. struct sk_buff *msg;
  534. struct fou_cfg cfg;
  535. struct fou *fout;
  536. __be16 port;
  537. int ret;
  538. ret = parse_nl_config(info, &cfg);
  539. if (ret)
  540. return ret;
  541. port = cfg.udp_config.local_udp_port;
  542. if (port == 0)
  543. return -EINVAL;
  544. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  545. if (!msg)
  546. return -ENOMEM;
  547. ret = -ESRCH;
  548. mutex_lock(&fn->fou_lock);
  549. list_for_each_entry(fout, &fn->fou_list, list) {
  550. if (port == fout->port) {
  551. ret = fou_dump_info(fout, info->snd_portid,
  552. info->snd_seq, 0, msg,
  553. info->genlhdr->cmd);
  554. break;
  555. }
  556. }
  557. mutex_unlock(&fn->fou_lock);
  558. if (ret < 0)
  559. goto out_free;
  560. return genlmsg_reply(msg, info);
  561. out_free:
  562. nlmsg_free(msg);
  563. return ret;
  564. }
  565. static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
  566. {
  567. struct net *net = sock_net(skb->sk);
  568. struct fou_net *fn = net_generic(net, fou_net_id);
  569. struct fou *fout;
  570. int idx = 0, ret;
  571. mutex_lock(&fn->fou_lock);
  572. list_for_each_entry(fout, &fn->fou_list, list) {
  573. if (idx++ < cb->args[0])
  574. continue;
  575. ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
  576. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  577. skb, FOU_CMD_GET);
  578. if (ret)
  579. break;
  580. }
  581. mutex_unlock(&fn->fou_lock);
  582. cb->args[0] = idx;
  583. return skb->len;
  584. }
  585. static const struct genl_ops fou_nl_ops[] = {
  586. {
  587. .cmd = FOU_CMD_ADD,
  588. .doit = fou_nl_cmd_add_port,
  589. .policy = fou_nl_policy,
  590. .flags = GENL_ADMIN_PERM,
  591. },
  592. {
  593. .cmd = FOU_CMD_DEL,
  594. .doit = fou_nl_cmd_rm_port,
  595. .policy = fou_nl_policy,
  596. .flags = GENL_ADMIN_PERM,
  597. },
  598. {
  599. .cmd = FOU_CMD_GET,
  600. .doit = fou_nl_cmd_get_port,
  601. .dumpit = fou_nl_dump,
  602. .policy = fou_nl_policy,
  603. },
  604. };
  605. size_t fou_encap_hlen(struct ip_tunnel_encap *e)
  606. {
  607. return sizeof(struct udphdr);
  608. }
  609. EXPORT_SYMBOL(fou_encap_hlen);
  610. size_t gue_encap_hlen(struct ip_tunnel_encap *e)
  611. {
  612. size_t len;
  613. bool need_priv = false;
  614. len = sizeof(struct udphdr) + sizeof(struct guehdr);
  615. if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
  616. len += GUE_PLEN_REMCSUM;
  617. need_priv = true;
  618. }
  619. len += need_priv ? GUE_LEN_PRIV : 0;
  620. return len;
  621. }
  622. EXPORT_SYMBOL(gue_encap_hlen);
  623. static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
  624. struct flowi4 *fl4, u8 *protocol, __be16 sport)
  625. {
  626. struct udphdr *uh;
  627. skb_push(skb, sizeof(struct udphdr));
  628. skb_reset_transport_header(skb);
  629. uh = udp_hdr(skb);
  630. uh->dest = e->dport;
  631. uh->source = sport;
  632. uh->len = htons(skb->len);
  633. uh->check = 0;
  634. udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
  635. fl4->saddr, fl4->daddr, skb->len);
  636. *protocol = IPPROTO_UDP;
  637. }
  638. int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  639. u8 *protocol, struct flowi4 *fl4)
  640. {
  641. bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM);
  642. int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  643. __be16 sport;
  644. skb = iptunnel_handle_offloads(skb, csum, type);
  645. if (IS_ERR(skb))
  646. return PTR_ERR(skb);
  647. sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
  648. skb, 0, 0, false);
  649. fou_build_udp(skb, e, fl4, protocol, sport);
  650. return 0;
  651. }
  652. EXPORT_SYMBOL(fou_build_header);
  653. int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  654. u8 *protocol, struct flowi4 *fl4)
  655. {
  656. bool csum = !!(e->flags & TUNNEL_ENCAP_FLAG_CSUM);
  657. int type = csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  658. struct guehdr *guehdr;
  659. size_t hdrlen, optlen = 0;
  660. __be16 sport;
  661. void *data;
  662. bool need_priv = false;
  663. if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
  664. skb->ip_summed == CHECKSUM_PARTIAL) {
  665. csum = false;
  666. optlen += GUE_PLEN_REMCSUM;
  667. type |= SKB_GSO_TUNNEL_REMCSUM;
  668. need_priv = true;
  669. }
  670. optlen += need_priv ? GUE_LEN_PRIV : 0;
  671. skb = iptunnel_handle_offloads(skb, csum, type);
  672. if (IS_ERR(skb))
  673. return PTR_ERR(skb);
  674. /* Get source port (based on flow hash) before skb_push */
  675. sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
  676. skb, 0, 0, false);
  677. hdrlen = sizeof(struct guehdr) + optlen;
  678. skb_push(skb, hdrlen);
  679. guehdr = (struct guehdr *)skb->data;
  680. guehdr->control = 0;
  681. guehdr->version = 0;
  682. guehdr->hlen = optlen >> 2;
  683. guehdr->flags = 0;
  684. guehdr->proto_ctype = *protocol;
  685. data = &guehdr[1];
  686. if (need_priv) {
  687. __be32 *flags = data;
  688. guehdr->flags |= GUE_FLAG_PRIV;
  689. *flags = 0;
  690. data += GUE_LEN_PRIV;
  691. if (type & SKB_GSO_TUNNEL_REMCSUM) {
  692. u16 csum_start = skb_checksum_start_offset(skb);
  693. __be16 *pd = data;
  694. if (csum_start < hdrlen)
  695. return -EINVAL;
  696. csum_start -= hdrlen;
  697. pd[0] = htons(csum_start);
  698. pd[1] = htons(csum_start + skb->csum_offset);
  699. if (!skb_is_gso(skb)) {
  700. skb->ip_summed = CHECKSUM_NONE;
  701. skb->encapsulation = 0;
  702. }
  703. *flags |= GUE_PFLAG_REMCSUM;
  704. data += GUE_PLEN_REMCSUM;
  705. }
  706. }
  707. fou_build_udp(skb, e, fl4, protocol, sport);
  708. return 0;
  709. }
  710. EXPORT_SYMBOL(gue_build_header);
  711. #ifdef CONFIG_NET_FOU_IP_TUNNELS
  712. static const struct ip_tunnel_encap_ops fou_iptun_ops = {
  713. .encap_hlen = fou_encap_hlen,
  714. .build_header = fou_build_header,
  715. };
  716. static const struct ip_tunnel_encap_ops gue_iptun_ops = {
  717. .encap_hlen = gue_encap_hlen,
  718. .build_header = gue_build_header,
  719. };
  720. static int ip_tunnel_encap_add_fou_ops(void)
  721. {
  722. int ret;
  723. ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
  724. if (ret < 0) {
  725. pr_err("can't add fou ops\n");
  726. return ret;
  727. }
  728. ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
  729. if (ret < 0) {
  730. pr_err("can't add gue ops\n");
  731. ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
  732. return ret;
  733. }
  734. return 0;
  735. }
  736. static void ip_tunnel_encap_del_fou_ops(void)
  737. {
  738. ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
  739. ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
  740. }
  741. #else
  742. static int ip_tunnel_encap_add_fou_ops(void)
  743. {
  744. return 0;
  745. }
  746. static void ip_tunnel_encap_del_fou_ops(void)
  747. {
  748. }
  749. #endif
  750. static __net_init int fou_init_net(struct net *net)
  751. {
  752. struct fou_net *fn = net_generic(net, fou_net_id);
  753. INIT_LIST_HEAD(&fn->fou_list);
  754. mutex_init(&fn->fou_lock);
  755. return 0;
  756. }
  757. static __net_exit void fou_exit_net(struct net *net)
  758. {
  759. struct fou_net *fn = net_generic(net, fou_net_id);
  760. struct fou *fou, *next;
  761. /* Close all the FOU sockets */
  762. mutex_lock(&fn->fou_lock);
  763. list_for_each_entry_safe(fou, next, &fn->fou_list, list)
  764. fou_release(fou);
  765. mutex_unlock(&fn->fou_lock);
  766. }
  767. static struct pernet_operations fou_net_ops = {
  768. .init = fou_init_net,
  769. .exit = fou_exit_net,
  770. .id = &fou_net_id,
  771. .size = sizeof(struct fou_net),
  772. };
  773. static int __init fou_init(void)
  774. {
  775. int ret;
  776. ret = register_pernet_device(&fou_net_ops);
  777. if (ret)
  778. goto exit;
  779. ret = genl_register_family_with_ops(&fou_nl_family,
  780. fou_nl_ops);
  781. if (ret < 0)
  782. goto unregister;
  783. ret = ip_tunnel_encap_add_fou_ops();
  784. if (ret == 0)
  785. return 0;
  786. genl_unregister_family(&fou_nl_family);
  787. unregister:
  788. unregister_pernet_device(&fou_net_ops);
  789. exit:
  790. return ret;
  791. }
  792. static void __exit fou_fini(void)
  793. {
  794. ip_tunnel_encap_del_fou_ops();
  795. genl_unregister_family(&fou_nl_family);
  796. unregister_pernet_device(&fou_net_ops);
  797. }
  798. module_init(fou_init);
  799. module_exit(fou_fini);
  800. MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
  801. MODULE_LICENSE("GPL");