geneve.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * GENEVE: Generic Network Virtualization Encapsulation
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/hash.h>
  16. #include <net/dst_metadata.h>
  17. #include <net/gro_cells.h>
  18. #include <net/rtnetlink.h>
  19. #include <net/geneve.h>
  20. #include <net/protocol.h>
  21. #define GENEVE_NETDEV_VER "0.6"
  22. #define GENEVE_UDP_PORT 6081
  23. #define GENEVE_N_VID (1u << 24)
  24. #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
  25. #define VNI_HASH_BITS 10
  26. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  27. static bool log_ecn_error = true;
  28. module_param(log_ecn_error, bool, 0644);
  29. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  30. #define GENEVE_VER 0
  31. #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
  32. /* per-network namespace private data for this module */
  33. struct geneve_net {
  34. struct list_head geneve_list;
  35. struct list_head sock_list;
  36. };
  37. static int geneve_net_id;
  38. union geneve_addr {
  39. struct sockaddr_in sin;
  40. struct sockaddr_in6 sin6;
  41. struct sockaddr sa;
  42. };
  43. static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
  44. /* Pseudo network device */
  45. struct geneve_dev {
  46. struct hlist_node hlist; /* vni hash table */
  47. struct net *net; /* netns for packet i/o */
  48. struct net_device *dev; /* netdev for geneve tunnel */
  49. struct geneve_sock *sock4; /* IPv4 socket used for geneve tunnel */
  50. #if IS_ENABLED(CONFIG_IPV6)
  51. struct geneve_sock *sock6; /* IPv6 socket used for geneve tunnel */
  52. #endif
  53. u8 vni[3]; /* virtual network ID for tunnel */
  54. u8 ttl; /* TTL override */
  55. u8 tos; /* TOS override */
  56. union geneve_addr remote; /* IP address for link partner */
  57. struct list_head next; /* geneve's per namespace list */
  58. __be16 dst_port;
  59. bool collect_md;
  60. struct gro_cells gro_cells;
  61. };
  62. struct geneve_sock {
  63. bool collect_md;
  64. struct list_head list;
  65. struct socket *sock;
  66. struct rcu_head rcu;
  67. int refcnt;
  68. struct udp_offload udp_offloads;
  69. struct hlist_head vni_list[VNI_HASH_SIZE];
  70. };
  71. static inline __u32 geneve_net_vni_hash(u8 vni[3])
  72. {
  73. __u32 vnid;
  74. vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
  75. return hash_32(vnid, VNI_HASH_BITS);
  76. }
  77. static __be64 vni_to_tunnel_id(const __u8 *vni)
  78. {
  79. #ifdef __BIG_ENDIAN
  80. return (vni[0] << 16) | (vni[1] << 8) | vni[2];
  81. #else
  82. return (__force __be64)(((__force u64)vni[0] << 40) |
  83. ((__force u64)vni[1] << 48) |
  84. ((__force u64)vni[2] << 56));
  85. #endif
  86. }
  87. static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
  88. __be32 addr, u8 vni[])
  89. {
  90. struct hlist_head *vni_list_head;
  91. struct geneve_dev *geneve;
  92. __u32 hash;
  93. /* Find the device for this VNI */
  94. hash = geneve_net_vni_hash(vni);
  95. vni_list_head = &gs->vni_list[hash];
  96. hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
  97. if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
  98. addr == geneve->remote.sin.sin_addr.s_addr)
  99. return geneve;
  100. }
  101. return NULL;
  102. }
  103. #if IS_ENABLED(CONFIG_IPV6)
  104. static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
  105. struct in6_addr addr6, u8 vni[])
  106. {
  107. struct hlist_head *vni_list_head;
  108. struct geneve_dev *geneve;
  109. __u32 hash;
  110. /* Find the device for this VNI */
  111. hash = geneve_net_vni_hash(vni);
  112. vni_list_head = &gs->vni_list[hash];
  113. hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
  114. if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
  115. ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
  116. return geneve;
  117. }
  118. return NULL;
  119. }
  120. #endif
  121. static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
  122. {
  123. return (struct genevehdr *)(udp_hdr(skb) + 1);
  124. }
  125. /* geneve receive/decap routine */
  126. static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb)
  127. {
  128. struct genevehdr *gnvh = geneve_hdr(skb);
  129. struct metadata_dst *tun_dst = NULL;
  130. struct geneve_dev *geneve = NULL;
  131. struct pcpu_sw_netstats *stats;
  132. struct iphdr *iph = NULL;
  133. __be32 addr;
  134. static u8 zero_vni[3];
  135. u8 *vni;
  136. int err = 0;
  137. sa_family_t sa_family;
  138. #if IS_ENABLED(CONFIG_IPV6)
  139. struct ipv6hdr *ip6h = NULL;
  140. struct in6_addr addr6;
  141. static struct in6_addr zero_addr6;
  142. #endif
  143. sa_family = gs->sock->sk->sk_family;
  144. if (sa_family == AF_INET) {
  145. iph = ip_hdr(skb); /* outer IP header... */
  146. if (gs->collect_md) {
  147. vni = zero_vni;
  148. addr = 0;
  149. } else {
  150. vni = gnvh->vni;
  151. addr = iph->saddr;
  152. }
  153. geneve = geneve_lookup(gs, addr, vni);
  154. #if IS_ENABLED(CONFIG_IPV6)
  155. } else if (sa_family == AF_INET6) {
  156. ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
  157. if (gs->collect_md) {
  158. vni = zero_vni;
  159. addr6 = zero_addr6;
  160. } else {
  161. vni = gnvh->vni;
  162. addr6 = ip6h->saddr;
  163. }
  164. geneve = geneve6_lookup(gs, addr6, vni);
  165. #endif
  166. }
  167. if (!geneve)
  168. goto drop;
  169. if (ip_tunnel_collect_metadata() || gs->collect_md) {
  170. __be16 flags;
  171. flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
  172. (gnvh->oam ? TUNNEL_OAM : 0) |
  173. (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
  174. tun_dst = udp_tun_rx_dst(skb, sa_family, flags,
  175. vni_to_tunnel_id(gnvh->vni),
  176. gnvh->opt_len * 4);
  177. if (!tun_dst)
  178. goto drop;
  179. /* Update tunnel dst according to Geneve options. */
  180. ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
  181. gnvh->options, gnvh->opt_len * 4);
  182. } else {
  183. /* Drop packets w/ critical options,
  184. * since we don't support any...
  185. */
  186. if (gnvh->critical)
  187. goto drop;
  188. }
  189. skb_reset_mac_header(skb);
  190. skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev)));
  191. skb->protocol = eth_type_trans(skb, geneve->dev);
  192. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  193. if (tun_dst)
  194. skb_dst_set(skb, &tun_dst->dst);
  195. /* Ignore packet loops (and multicast echo) */
  196. if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
  197. goto drop;
  198. skb_reset_network_header(skb);
  199. if (iph)
  200. err = IP_ECN_decapsulate(iph, skb);
  201. #if IS_ENABLED(CONFIG_IPV6)
  202. if (ip6h)
  203. err = IP6_ECN_decapsulate(ip6h, skb);
  204. #endif
  205. if (unlikely(err)) {
  206. if (log_ecn_error) {
  207. if (iph)
  208. net_info_ratelimited("non-ECT from %pI4 "
  209. "with TOS=%#x\n",
  210. &iph->saddr, iph->tos);
  211. #if IS_ENABLED(CONFIG_IPV6)
  212. if (ip6h)
  213. net_info_ratelimited("non-ECT from %pI6\n",
  214. &ip6h->saddr);
  215. #endif
  216. }
  217. if (err > 1) {
  218. ++geneve->dev->stats.rx_frame_errors;
  219. ++geneve->dev->stats.rx_errors;
  220. goto drop;
  221. }
  222. }
  223. stats = this_cpu_ptr(geneve->dev->tstats);
  224. u64_stats_update_begin(&stats->syncp);
  225. stats->rx_packets++;
  226. stats->rx_bytes += skb->len;
  227. u64_stats_update_end(&stats->syncp);
  228. gro_cells_receive(&geneve->gro_cells, skb);
  229. return;
  230. drop:
  231. /* Consume bad packet */
  232. kfree_skb(skb);
  233. }
  234. /* Setup stats when device is created */
  235. static int geneve_init(struct net_device *dev)
  236. {
  237. struct geneve_dev *geneve = netdev_priv(dev);
  238. int err;
  239. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  240. if (!dev->tstats)
  241. return -ENOMEM;
  242. err = gro_cells_init(&geneve->gro_cells, dev);
  243. if (err) {
  244. free_percpu(dev->tstats);
  245. return err;
  246. }
  247. return 0;
  248. }
  249. static void geneve_uninit(struct net_device *dev)
  250. {
  251. struct geneve_dev *geneve = netdev_priv(dev);
  252. gro_cells_destroy(&geneve->gro_cells);
  253. free_percpu(dev->tstats);
  254. }
  255. /* Callback from net/ipv4/udp.c to receive packets */
  256. static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  257. {
  258. struct genevehdr *geneveh;
  259. struct geneve_sock *gs;
  260. int opts_len;
  261. /* Need Geneve and inner Ethernet header to be present */
  262. if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
  263. goto drop;
  264. /* Return packets with reserved bits set */
  265. geneveh = geneve_hdr(skb);
  266. if (unlikely(geneveh->ver != GENEVE_VER))
  267. goto drop;
  268. if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
  269. goto drop;
  270. opts_len = geneveh->opt_len * 4;
  271. if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
  272. htons(ETH_P_TEB)))
  273. goto drop;
  274. gs = rcu_dereference_sk_user_data(sk);
  275. if (!gs)
  276. goto drop;
  277. geneve_rx(gs, skb);
  278. return 0;
  279. drop:
  280. /* Consume bad packet */
  281. kfree_skb(skb);
  282. return 0;
  283. }
  284. static struct socket *geneve_create_sock(struct net *net, bool ipv6,
  285. __be16 port)
  286. {
  287. struct socket *sock;
  288. struct udp_port_cfg udp_conf;
  289. int err;
  290. memset(&udp_conf, 0, sizeof(udp_conf));
  291. if (ipv6) {
  292. udp_conf.family = AF_INET6;
  293. udp_conf.ipv6_v6only = 1;
  294. } else {
  295. udp_conf.family = AF_INET;
  296. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  297. }
  298. udp_conf.local_udp_port = port;
  299. /* Open UDP socket */
  300. err = udp_sock_create(net, &udp_conf, &sock);
  301. if (err < 0)
  302. return ERR_PTR(err);
  303. return sock;
  304. }
  305. static void geneve_notify_add_rx_port(struct geneve_sock *gs)
  306. {
  307. struct sock *sk = gs->sock->sk;
  308. sa_family_t sa_family = sk->sk_family;
  309. int err;
  310. if (sa_family == AF_INET) {
  311. err = udp_add_offload(&gs->udp_offloads);
  312. if (err)
  313. pr_warn("geneve: udp_add_offload failed with status %d\n",
  314. err);
  315. }
  316. }
  317. static int geneve_hlen(struct genevehdr *gh)
  318. {
  319. return sizeof(*gh) + gh->opt_len * 4;
  320. }
  321. static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
  322. struct sk_buff *skb,
  323. struct udp_offload *uoff)
  324. {
  325. struct sk_buff *p, **pp = NULL;
  326. struct genevehdr *gh, *gh2;
  327. unsigned int hlen, gh_len, off_gnv;
  328. const struct packet_offload *ptype;
  329. __be16 type;
  330. int flush = 1;
  331. off_gnv = skb_gro_offset(skb);
  332. hlen = off_gnv + sizeof(*gh);
  333. gh = skb_gro_header_fast(skb, off_gnv);
  334. if (skb_gro_header_hard(skb, hlen)) {
  335. gh = skb_gro_header_slow(skb, hlen, off_gnv);
  336. if (unlikely(!gh))
  337. goto out;
  338. }
  339. if (gh->ver != GENEVE_VER || gh->oam)
  340. goto out;
  341. gh_len = geneve_hlen(gh);
  342. hlen = off_gnv + gh_len;
  343. if (skb_gro_header_hard(skb, hlen)) {
  344. gh = skb_gro_header_slow(skb, hlen, off_gnv);
  345. if (unlikely(!gh))
  346. goto out;
  347. }
  348. flush = 0;
  349. for (p = *head; p; p = p->next) {
  350. if (!NAPI_GRO_CB(p)->same_flow)
  351. continue;
  352. gh2 = (struct genevehdr *)(p->data + off_gnv);
  353. if (gh->opt_len != gh2->opt_len ||
  354. memcmp(gh, gh2, gh_len)) {
  355. NAPI_GRO_CB(p)->same_flow = 0;
  356. continue;
  357. }
  358. }
  359. type = gh->proto_type;
  360. rcu_read_lock();
  361. ptype = gro_find_receive_by_type(type);
  362. if (!ptype) {
  363. flush = 1;
  364. goto out_unlock;
  365. }
  366. skb_gro_pull(skb, gh_len);
  367. skb_gro_postpull_rcsum(skb, gh, gh_len);
  368. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  369. out_unlock:
  370. rcu_read_unlock();
  371. out:
  372. NAPI_GRO_CB(skb)->flush |= flush;
  373. return pp;
  374. }
  375. static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
  376. struct udp_offload *uoff)
  377. {
  378. struct genevehdr *gh;
  379. struct packet_offload *ptype;
  380. __be16 type;
  381. int gh_len;
  382. int err = -ENOSYS;
  383. udp_tunnel_gro_complete(skb, nhoff);
  384. gh = (struct genevehdr *)(skb->data + nhoff);
  385. gh_len = geneve_hlen(gh);
  386. type = gh->proto_type;
  387. rcu_read_lock();
  388. ptype = gro_find_complete_by_type(type);
  389. if (ptype)
  390. err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
  391. rcu_read_unlock();
  392. return err;
  393. }
  394. /* Create new listen socket if needed */
  395. static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
  396. bool ipv6)
  397. {
  398. struct geneve_net *gn = net_generic(net, geneve_net_id);
  399. struct geneve_sock *gs;
  400. struct socket *sock;
  401. struct udp_tunnel_sock_cfg tunnel_cfg;
  402. int h;
  403. gs = kzalloc(sizeof(*gs), GFP_KERNEL);
  404. if (!gs)
  405. return ERR_PTR(-ENOMEM);
  406. sock = geneve_create_sock(net, ipv6, port);
  407. if (IS_ERR(sock)) {
  408. kfree(gs);
  409. return ERR_CAST(sock);
  410. }
  411. gs->sock = sock;
  412. gs->refcnt = 1;
  413. for (h = 0; h < VNI_HASH_SIZE; ++h)
  414. INIT_HLIST_HEAD(&gs->vni_list[h]);
  415. /* Initialize the geneve udp offloads structure */
  416. gs->udp_offloads.port = port;
  417. gs->udp_offloads.callbacks.gro_receive = geneve_gro_receive;
  418. gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
  419. geneve_notify_add_rx_port(gs);
  420. /* Mark socket as an encapsulation socket */
  421. tunnel_cfg.sk_user_data = gs;
  422. tunnel_cfg.encap_type = 1;
  423. tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
  424. tunnel_cfg.encap_destroy = NULL;
  425. setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
  426. list_add(&gs->list, &gn->sock_list);
  427. return gs;
  428. }
  429. static void geneve_notify_del_rx_port(struct geneve_sock *gs)
  430. {
  431. struct sock *sk = gs->sock->sk;
  432. sa_family_t sa_family = sk->sk_family;
  433. if (sa_family == AF_INET)
  434. udp_del_offload(&gs->udp_offloads);
  435. }
  436. static void __geneve_sock_release(struct geneve_sock *gs)
  437. {
  438. if (!gs || --gs->refcnt)
  439. return;
  440. list_del(&gs->list);
  441. geneve_notify_del_rx_port(gs);
  442. udp_tunnel_sock_release(gs->sock);
  443. kfree_rcu(gs, rcu);
  444. }
  445. static void geneve_sock_release(struct geneve_dev *geneve)
  446. {
  447. __geneve_sock_release(geneve->sock4);
  448. #if IS_ENABLED(CONFIG_IPV6)
  449. __geneve_sock_release(geneve->sock6);
  450. #endif
  451. }
  452. static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
  453. sa_family_t family,
  454. __be16 dst_port)
  455. {
  456. struct geneve_sock *gs;
  457. list_for_each_entry(gs, &gn->sock_list, list) {
  458. if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
  459. inet_sk(gs->sock->sk)->sk.sk_family == family) {
  460. return gs;
  461. }
  462. }
  463. return NULL;
  464. }
  465. static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
  466. {
  467. struct net *net = geneve->net;
  468. struct geneve_net *gn = net_generic(net, geneve_net_id);
  469. struct geneve_sock *gs;
  470. __u32 hash;
  471. gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
  472. if (gs) {
  473. gs->refcnt++;
  474. goto out;
  475. }
  476. gs = geneve_socket_create(net, geneve->dst_port, ipv6);
  477. if (IS_ERR(gs))
  478. return PTR_ERR(gs);
  479. out:
  480. gs->collect_md = geneve->collect_md;
  481. #if IS_ENABLED(CONFIG_IPV6)
  482. if (ipv6)
  483. geneve->sock6 = gs;
  484. else
  485. #endif
  486. geneve->sock4 = gs;
  487. hash = geneve_net_vni_hash(geneve->vni);
  488. hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
  489. return 0;
  490. }
  491. static int geneve_open(struct net_device *dev)
  492. {
  493. struct geneve_dev *geneve = netdev_priv(dev);
  494. bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
  495. bool metadata = geneve->collect_md;
  496. int ret = 0;
  497. geneve->sock4 = NULL;
  498. #if IS_ENABLED(CONFIG_IPV6)
  499. geneve->sock6 = NULL;
  500. if (ipv6 || metadata)
  501. ret = geneve_sock_add(geneve, true);
  502. #endif
  503. if (!ret && (!ipv6 || metadata))
  504. ret = geneve_sock_add(geneve, false);
  505. if (ret < 0)
  506. geneve_sock_release(geneve);
  507. return ret;
  508. }
  509. static int geneve_stop(struct net_device *dev)
  510. {
  511. struct geneve_dev *geneve = netdev_priv(dev);
  512. if (!hlist_unhashed(&geneve->hlist))
  513. hlist_del_rcu(&geneve->hlist);
  514. geneve_sock_release(geneve);
  515. return 0;
  516. }
  517. static void geneve_build_header(struct genevehdr *geneveh,
  518. __be16 tun_flags, u8 vni[3],
  519. u8 options_len, u8 *options)
  520. {
  521. geneveh->ver = GENEVE_VER;
  522. geneveh->opt_len = options_len / 4;
  523. geneveh->oam = !!(tun_flags & TUNNEL_OAM);
  524. geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
  525. geneveh->rsvd1 = 0;
  526. memcpy(geneveh->vni, vni, 3);
  527. geneveh->proto_type = htons(ETH_P_TEB);
  528. geneveh->rsvd2 = 0;
  529. memcpy(geneveh->options, options, options_len);
  530. }
  531. static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
  532. __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
  533. bool csum, bool xnet)
  534. {
  535. struct genevehdr *gnvh;
  536. int min_headroom;
  537. int err;
  538. skb_scrub_packet(skb, xnet);
  539. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  540. + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
  541. err = skb_cow_head(skb, min_headroom);
  542. if (unlikely(err)) {
  543. kfree_skb(skb);
  544. goto free_rt;
  545. }
  546. skb = udp_tunnel_handle_offloads(skb, csum);
  547. if (IS_ERR(skb)) {
  548. err = PTR_ERR(skb);
  549. goto free_rt;
  550. }
  551. gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
  552. geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
  553. skb_set_inner_protocol(skb, htons(ETH_P_TEB));
  554. return 0;
  555. free_rt:
  556. ip_rt_put(rt);
  557. return err;
  558. }
  559. #if IS_ENABLED(CONFIG_IPV6)
  560. static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
  561. __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
  562. bool csum, bool xnet)
  563. {
  564. struct genevehdr *gnvh;
  565. int min_headroom;
  566. int err;
  567. skb_scrub_packet(skb, xnet);
  568. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  569. + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
  570. err = skb_cow_head(skb, min_headroom);
  571. if (unlikely(err)) {
  572. kfree_skb(skb);
  573. goto free_dst;
  574. }
  575. skb = udp_tunnel_handle_offloads(skb, csum);
  576. if (IS_ERR(skb)) {
  577. err = PTR_ERR(skb);
  578. goto free_dst;
  579. }
  580. gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
  581. geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
  582. skb_set_inner_protocol(skb, htons(ETH_P_TEB));
  583. return 0;
  584. free_dst:
  585. dst_release(dst);
  586. return err;
  587. }
  588. #endif
  589. static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
  590. struct net_device *dev,
  591. struct flowi4 *fl4,
  592. struct ip_tunnel_info *info)
  593. {
  594. struct geneve_dev *geneve = netdev_priv(dev);
  595. struct rtable *rt = NULL;
  596. __u8 tos;
  597. memset(fl4, 0, sizeof(*fl4));
  598. fl4->flowi4_mark = skb->mark;
  599. fl4->flowi4_proto = IPPROTO_UDP;
  600. if (info) {
  601. fl4->daddr = info->key.u.ipv4.dst;
  602. fl4->saddr = info->key.u.ipv4.src;
  603. fl4->flowi4_tos = RT_TOS(info->key.tos);
  604. } else {
  605. tos = geneve->tos;
  606. if (tos == 1) {
  607. const struct iphdr *iip = ip_hdr(skb);
  608. tos = ip_tunnel_get_dsfield(iip, skb);
  609. }
  610. fl4->flowi4_tos = RT_TOS(tos);
  611. fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
  612. }
  613. rt = ip_route_output_key(geneve->net, fl4);
  614. if (IS_ERR(rt)) {
  615. netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
  616. return ERR_PTR(-ENETUNREACH);
  617. }
  618. if (rt->dst.dev == dev) { /* is this necessary? */
  619. netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
  620. ip_rt_put(rt);
  621. return ERR_PTR(-ELOOP);
  622. }
  623. return rt;
  624. }
  625. #if IS_ENABLED(CONFIG_IPV6)
  626. static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
  627. struct net_device *dev,
  628. struct flowi6 *fl6,
  629. struct ip_tunnel_info *info)
  630. {
  631. struct geneve_dev *geneve = netdev_priv(dev);
  632. struct geneve_sock *gs6 = geneve->sock6;
  633. struct dst_entry *dst = NULL;
  634. __u8 prio;
  635. memset(fl6, 0, sizeof(*fl6));
  636. fl6->flowi6_mark = skb->mark;
  637. fl6->flowi6_proto = IPPROTO_UDP;
  638. if (info) {
  639. fl6->daddr = info->key.u.ipv6.dst;
  640. fl6->saddr = info->key.u.ipv6.src;
  641. fl6->flowi6_tos = RT_TOS(info->key.tos);
  642. } else {
  643. prio = geneve->tos;
  644. if (prio == 1) {
  645. const struct iphdr *iip = ip_hdr(skb);
  646. prio = ip_tunnel_get_dsfield(iip, skb);
  647. }
  648. fl6->flowi6_tos = RT_TOS(prio);
  649. fl6->daddr = geneve->remote.sin6.sin6_addr;
  650. }
  651. if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
  652. netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
  653. return ERR_PTR(-ENETUNREACH);
  654. }
  655. if (dst->dev == dev) { /* is this necessary? */
  656. netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
  657. dst_release(dst);
  658. return ERR_PTR(-ELOOP);
  659. }
  660. return dst;
  661. }
  662. #endif
  663. /* Convert 64 bit tunnel ID to 24 bit VNI. */
  664. static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
  665. {
  666. #ifdef __BIG_ENDIAN
  667. vni[0] = (__force __u8)(tun_id >> 16);
  668. vni[1] = (__force __u8)(tun_id >> 8);
  669. vni[2] = (__force __u8)tun_id;
  670. #else
  671. vni[0] = (__force __u8)((__force u64)tun_id >> 40);
  672. vni[1] = (__force __u8)((__force u64)tun_id >> 48);
  673. vni[2] = (__force __u8)((__force u64)tun_id >> 56);
  674. #endif
  675. }
  676. static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
  677. struct ip_tunnel_info *info)
  678. {
  679. struct geneve_dev *geneve = netdev_priv(dev);
  680. struct geneve_sock *gs4 = geneve->sock4;
  681. struct rtable *rt = NULL;
  682. int err = -EINVAL;
  683. struct flowi4 fl4;
  684. __u8 tos, ttl;
  685. __be16 sport;
  686. bool udp_csum;
  687. __be16 df;
  688. bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
  689. if (geneve->collect_md) {
  690. if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
  691. netdev_dbg(dev, "no tunnel metadata\n");
  692. goto tx_error;
  693. }
  694. if (info && ip_tunnel_info_af(info) != AF_INET)
  695. goto tx_error;
  696. }
  697. rt = geneve_get_v4_rt(skb, dev, &fl4, info);
  698. if (IS_ERR(rt)) {
  699. err = PTR_ERR(rt);
  700. goto tx_error;
  701. }
  702. sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
  703. skb_reset_mac_header(skb);
  704. if (info) {
  705. const struct ip_tunnel_key *key = &info->key;
  706. u8 *opts = NULL;
  707. u8 vni[3];
  708. tunnel_id_to_vni(key->tun_id, vni);
  709. if (key->tun_flags & TUNNEL_GENEVE_OPT)
  710. opts = ip_tunnel_info_opts(info);
  711. udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
  712. err = geneve_build_skb(rt, skb, key->tun_flags, vni,
  713. info->options_len, opts, udp_csum, xnet);
  714. if (unlikely(err))
  715. goto err;
  716. tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
  717. ttl = key->ttl;
  718. df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
  719. } else {
  720. udp_csum = false;
  721. err = geneve_build_skb(rt, skb, 0, geneve->vni,
  722. 0, NULL, udp_csum, xnet);
  723. if (unlikely(err))
  724. goto err;
  725. tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
  726. ttl = geneve->ttl;
  727. if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
  728. ttl = 1;
  729. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  730. df = 0;
  731. }
  732. err = udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
  733. tos, ttl, df, sport, geneve->dst_port,
  734. !net_eq(geneve->net, dev_net(geneve->dev)),
  735. !udp_csum);
  736. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  737. return NETDEV_TX_OK;
  738. tx_error:
  739. dev_kfree_skb(skb);
  740. err:
  741. if (err == -ELOOP)
  742. dev->stats.collisions++;
  743. else if (err == -ENETUNREACH)
  744. dev->stats.tx_carrier_errors++;
  745. else
  746. dev->stats.tx_errors++;
  747. return NETDEV_TX_OK;
  748. }
  749. #if IS_ENABLED(CONFIG_IPV6)
  750. static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
  751. struct ip_tunnel_info *info)
  752. {
  753. struct geneve_dev *geneve = netdev_priv(dev);
  754. struct geneve_sock *gs6 = geneve->sock6;
  755. struct dst_entry *dst = NULL;
  756. int err = -EINVAL;
  757. struct flowi6 fl6;
  758. __u8 prio, ttl;
  759. __be16 sport;
  760. bool udp_csum;
  761. bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
  762. if (geneve->collect_md) {
  763. if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
  764. netdev_dbg(dev, "no tunnel metadata\n");
  765. goto tx_error;
  766. }
  767. }
  768. dst = geneve_get_v6_dst(skb, dev, &fl6, info);
  769. if (IS_ERR(dst)) {
  770. err = PTR_ERR(dst);
  771. goto tx_error;
  772. }
  773. sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
  774. skb_reset_mac_header(skb);
  775. if (info) {
  776. const struct ip_tunnel_key *key = &info->key;
  777. u8 *opts = NULL;
  778. u8 vni[3];
  779. tunnel_id_to_vni(key->tun_id, vni);
  780. if (key->tun_flags & TUNNEL_GENEVE_OPT)
  781. opts = ip_tunnel_info_opts(info);
  782. udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
  783. err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
  784. info->options_len, opts,
  785. udp_csum, xnet);
  786. if (unlikely(err))
  787. goto err;
  788. prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
  789. ttl = key->ttl;
  790. } else {
  791. udp_csum = false;
  792. err = geneve6_build_skb(dst, skb, 0, geneve->vni,
  793. 0, NULL, udp_csum, xnet);
  794. if (unlikely(err))
  795. goto err;
  796. prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, ip_hdr(skb), skb);
  797. ttl = geneve->ttl;
  798. if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
  799. ttl = 1;
  800. ttl = ttl ? : ip6_dst_hoplimit(dst);
  801. }
  802. err = udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
  803. &fl6.saddr, &fl6.daddr, prio, ttl,
  804. sport, geneve->dst_port, !udp_csum);
  805. return NETDEV_TX_OK;
  806. tx_error:
  807. dev_kfree_skb(skb);
  808. err:
  809. if (err == -ELOOP)
  810. dev->stats.collisions++;
  811. else if (err == -ENETUNREACH)
  812. dev->stats.tx_carrier_errors++;
  813. else
  814. dev->stats.tx_errors++;
  815. return NETDEV_TX_OK;
  816. }
  817. #endif
  818. static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
  819. {
  820. struct geneve_dev *geneve = netdev_priv(dev);
  821. struct ip_tunnel_info *info = NULL;
  822. if (geneve->collect_md)
  823. info = skb_tunnel_info(skb);
  824. #if IS_ENABLED(CONFIG_IPV6)
  825. if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
  826. (!info && geneve->remote.sa.sa_family == AF_INET6))
  827. return geneve6_xmit_skb(skb, dev, info);
  828. #endif
  829. return geneve_xmit_skb(skb, dev, info);
  830. }
  831. static int geneve_change_mtu(struct net_device *dev, int new_mtu)
  832. {
  833. /* GENEVE overhead is not fixed, so we can't enforce a more
  834. * precise max MTU.
  835. */
  836. if (new_mtu < 68 || new_mtu > IP_MAX_MTU)
  837. return -EINVAL;
  838. dev->mtu = new_mtu;
  839. return 0;
  840. }
  841. static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  842. {
  843. struct ip_tunnel_info *info = skb_tunnel_info(skb);
  844. struct geneve_dev *geneve = netdev_priv(dev);
  845. struct rtable *rt;
  846. struct flowi4 fl4;
  847. #if IS_ENABLED(CONFIG_IPV6)
  848. struct dst_entry *dst;
  849. struct flowi6 fl6;
  850. #endif
  851. if (ip_tunnel_info_af(info) == AF_INET) {
  852. rt = geneve_get_v4_rt(skb, dev, &fl4, info);
  853. if (IS_ERR(rt))
  854. return PTR_ERR(rt);
  855. ip_rt_put(rt);
  856. info->key.u.ipv4.src = fl4.saddr;
  857. #if IS_ENABLED(CONFIG_IPV6)
  858. } else if (ip_tunnel_info_af(info) == AF_INET6) {
  859. dst = geneve_get_v6_dst(skb, dev, &fl6, info);
  860. if (IS_ERR(dst))
  861. return PTR_ERR(dst);
  862. dst_release(dst);
  863. info->key.u.ipv6.src = fl6.saddr;
  864. #endif
  865. } else {
  866. return -EINVAL;
  867. }
  868. info->key.tp_src = udp_flow_src_port(geneve->net, skb,
  869. 1, USHRT_MAX, true);
  870. info->key.tp_dst = geneve->dst_port;
  871. return 0;
  872. }
  873. static const struct net_device_ops geneve_netdev_ops = {
  874. .ndo_init = geneve_init,
  875. .ndo_uninit = geneve_uninit,
  876. .ndo_open = geneve_open,
  877. .ndo_stop = geneve_stop,
  878. .ndo_start_xmit = geneve_xmit,
  879. .ndo_get_stats64 = ip_tunnel_get_stats64,
  880. .ndo_change_mtu = geneve_change_mtu,
  881. .ndo_validate_addr = eth_validate_addr,
  882. .ndo_set_mac_address = eth_mac_addr,
  883. .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
  884. };
  885. static void geneve_get_drvinfo(struct net_device *dev,
  886. struct ethtool_drvinfo *drvinfo)
  887. {
  888. strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
  889. strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
  890. }
  891. static const struct ethtool_ops geneve_ethtool_ops = {
  892. .get_drvinfo = geneve_get_drvinfo,
  893. .get_link = ethtool_op_get_link,
  894. };
  895. /* Info for udev, that this is a virtual tunnel endpoint */
  896. static struct device_type geneve_type = {
  897. .name = "geneve",
  898. };
  899. /* Initialize the device structure. */
  900. static void geneve_setup(struct net_device *dev)
  901. {
  902. ether_setup(dev);
  903. dev->netdev_ops = &geneve_netdev_ops;
  904. dev->ethtool_ops = &geneve_ethtool_ops;
  905. dev->destructor = free_netdev;
  906. SET_NETDEV_DEVTYPE(dev, &geneve_type);
  907. dev->features |= NETIF_F_LLTX;
  908. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  909. dev->features |= NETIF_F_RXCSUM;
  910. dev->features |= NETIF_F_GSO_SOFTWARE;
  911. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  912. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  913. netif_keep_dst(dev);
  914. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
  915. eth_hw_addr_random(dev);
  916. }
  917. static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
  918. [IFLA_GENEVE_ID] = { .type = NLA_U32 },
  919. [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  920. [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
  921. [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
  922. [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
  923. [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
  924. [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
  925. };
  926. static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
  927. {
  928. if (tb[IFLA_ADDRESS]) {
  929. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  930. return -EINVAL;
  931. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  932. return -EADDRNOTAVAIL;
  933. }
  934. if (!data)
  935. return -EINVAL;
  936. if (data[IFLA_GENEVE_ID]) {
  937. __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
  938. if (vni >= GENEVE_VID_MASK)
  939. return -ERANGE;
  940. }
  941. return 0;
  942. }
  943. static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
  944. __be16 dst_port,
  945. union geneve_addr *remote,
  946. u8 vni[],
  947. bool *tun_on_same_port,
  948. bool *tun_collect_md)
  949. {
  950. struct geneve_dev *geneve, *t;
  951. *tun_on_same_port = false;
  952. *tun_collect_md = false;
  953. t = NULL;
  954. list_for_each_entry(geneve, &gn->geneve_list, next) {
  955. if (geneve->dst_port == dst_port) {
  956. *tun_collect_md = geneve->collect_md;
  957. *tun_on_same_port = true;
  958. }
  959. if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
  960. !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
  961. dst_port == geneve->dst_port)
  962. t = geneve;
  963. }
  964. return t;
  965. }
  966. static int geneve_configure(struct net *net, struct net_device *dev,
  967. union geneve_addr *remote,
  968. __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
  969. bool metadata)
  970. {
  971. struct geneve_net *gn = net_generic(net, geneve_net_id);
  972. struct geneve_dev *t, *geneve = netdev_priv(dev);
  973. bool tun_collect_md, tun_on_same_port;
  974. int err, encap_len;
  975. if (!remote)
  976. return -EINVAL;
  977. if (metadata &&
  978. (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
  979. return -EINVAL;
  980. geneve->net = net;
  981. geneve->dev = dev;
  982. geneve->vni[0] = (vni & 0x00ff0000) >> 16;
  983. geneve->vni[1] = (vni & 0x0000ff00) >> 8;
  984. geneve->vni[2] = vni & 0x000000ff;
  985. if ((remote->sa.sa_family == AF_INET &&
  986. IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
  987. (remote->sa.sa_family == AF_INET6 &&
  988. ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
  989. return -EINVAL;
  990. geneve->remote = *remote;
  991. geneve->ttl = ttl;
  992. geneve->tos = tos;
  993. geneve->dst_port = dst_port;
  994. geneve->collect_md = metadata;
  995. t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
  996. &tun_on_same_port, &tun_collect_md);
  997. if (t)
  998. return -EBUSY;
  999. /* make enough headroom for basic scenario */
  1000. encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
  1001. if (remote->sa.sa_family == AF_INET)
  1002. encap_len += sizeof(struct iphdr);
  1003. else
  1004. encap_len += sizeof(struct ipv6hdr);
  1005. dev->needed_headroom = encap_len + ETH_HLEN;
  1006. if (metadata) {
  1007. if (tun_on_same_port)
  1008. return -EPERM;
  1009. } else {
  1010. if (tun_collect_md)
  1011. return -EPERM;
  1012. }
  1013. err = register_netdevice(dev);
  1014. if (err)
  1015. return err;
  1016. list_add(&geneve->next, &gn->geneve_list);
  1017. return 0;
  1018. }
  1019. static int geneve_newlink(struct net *net, struct net_device *dev,
  1020. struct nlattr *tb[], struct nlattr *data[])
  1021. {
  1022. __be16 dst_port = htons(GENEVE_UDP_PORT);
  1023. __u8 ttl = 0, tos = 0;
  1024. bool metadata = false;
  1025. union geneve_addr remote = geneve_remote_unspec;
  1026. __u32 vni = 0;
  1027. if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
  1028. return -EINVAL;
  1029. if (data[IFLA_GENEVE_REMOTE]) {
  1030. remote.sa.sa_family = AF_INET;
  1031. remote.sin.sin_addr.s_addr =
  1032. nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
  1033. }
  1034. if (data[IFLA_GENEVE_REMOTE6]) {
  1035. if (!IS_ENABLED(CONFIG_IPV6))
  1036. return -EPFNOSUPPORT;
  1037. remote.sa.sa_family = AF_INET6;
  1038. remote.sin6.sin6_addr =
  1039. nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
  1040. if (ipv6_addr_type(&remote.sin6.sin6_addr) &
  1041. IPV6_ADDR_LINKLOCAL) {
  1042. netdev_dbg(dev, "link-local remote is unsupported\n");
  1043. return -EINVAL;
  1044. }
  1045. }
  1046. if (data[IFLA_GENEVE_ID])
  1047. vni = nla_get_u32(data[IFLA_GENEVE_ID]);
  1048. if (data[IFLA_GENEVE_TTL])
  1049. ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
  1050. if (data[IFLA_GENEVE_TOS])
  1051. tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
  1052. if (data[IFLA_GENEVE_PORT])
  1053. dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
  1054. if (data[IFLA_GENEVE_COLLECT_METADATA])
  1055. metadata = true;
  1056. return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
  1057. metadata);
  1058. }
  1059. static void geneve_dellink(struct net_device *dev, struct list_head *head)
  1060. {
  1061. struct geneve_dev *geneve = netdev_priv(dev);
  1062. list_del(&geneve->next);
  1063. unregister_netdevice_queue(dev, head);
  1064. }
  1065. static size_t geneve_get_size(const struct net_device *dev)
  1066. {
  1067. return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
  1068. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
  1069. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
  1070. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
  1071. nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
  1072. nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
  1073. 0;
  1074. }
  1075. static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1076. {
  1077. struct geneve_dev *geneve = netdev_priv(dev);
  1078. __u32 vni;
  1079. vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
  1080. if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
  1081. goto nla_put_failure;
  1082. if (geneve->remote.sa.sa_family == AF_INET) {
  1083. if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
  1084. geneve->remote.sin.sin_addr.s_addr))
  1085. goto nla_put_failure;
  1086. #if IS_ENABLED(CONFIG_IPV6)
  1087. } else {
  1088. if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
  1089. &geneve->remote.sin6.sin6_addr))
  1090. goto nla_put_failure;
  1091. #endif
  1092. }
  1093. if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
  1094. nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
  1095. goto nla_put_failure;
  1096. if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
  1097. goto nla_put_failure;
  1098. if (geneve->collect_md) {
  1099. if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
  1100. goto nla_put_failure;
  1101. }
  1102. return 0;
  1103. nla_put_failure:
  1104. return -EMSGSIZE;
  1105. }
  1106. static struct rtnl_link_ops geneve_link_ops __read_mostly = {
  1107. .kind = "geneve",
  1108. .maxtype = IFLA_GENEVE_MAX,
  1109. .policy = geneve_policy,
  1110. .priv_size = sizeof(struct geneve_dev),
  1111. .setup = geneve_setup,
  1112. .validate = geneve_validate,
  1113. .newlink = geneve_newlink,
  1114. .dellink = geneve_dellink,
  1115. .get_size = geneve_get_size,
  1116. .fill_info = geneve_fill_info,
  1117. };
  1118. struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
  1119. u8 name_assign_type, u16 dst_port)
  1120. {
  1121. struct nlattr *tb[IFLA_MAX + 1];
  1122. struct net_device *dev;
  1123. int err;
  1124. memset(tb, 0, sizeof(tb));
  1125. dev = rtnl_create_link(net, name, name_assign_type,
  1126. &geneve_link_ops, tb);
  1127. if (IS_ERR(dev))
  1128. return dev;
  1129. err = geneve_configure(net, dev, &geneve_remote_unspec,
  1130. 0, 0, 0, htons(dst_port), true);
  1131. if (err)
  1132. goto err;
  1133. /* openvswitch users expect packet sizes to be unrestricted,
  1134. * so set the largest MTU we can.
  1135. */
  1136. err = geneve_change_mtu(dev, IP_MAX_MTU);
  1137. if (err)
  1138. goto err;
  1139. return dev;
  1140. err:
  1141. free_netdev(dev);
  1142. return ERR_PTR(err);
  1143. }
  1144. EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
  1145. static __net_init int geneve_init_net(struct net *net)
  1146. {
  1147. struct geneve_net *gn = net_generic(net, geneve_net_id);
  1148. INIT_LIST_HEAD(&gn->geneve_list);
  1149. INIT_LIST_HEAD(&gn->sock_list);
  1150. return 0;
  1151. }
  1152. static void __net_exit geneve_exit_net(struct net *net)
  1153. {
  1154. struct geneve_net *gn = net_generic(net, geneve_net_id);
  1155. struct geneve_dev *geneve, *next;
  1156. struct net_device *dev, *aux;
  1157. LIST_HEAD(list);
  1158. rtnl_lock();
  1159. /* gather any geneve devices that were moved into this ns */
  1160. for_each_netdev_safe(net, dev, aux)
  1161. if (dev->rtnl_link_ops == &geneve_link_ops)
  1162. unregister_netdevice_queue(dev, &list);
  1163. /* now gather any other geneve devices that were created in this ns */
  1164. list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
  1165. /* If geneve->dev is in the same netns, it was already added
  1166. * to the list by the previous loop.
  1167. */
  1168. if (!net_eq(dev_net(geneve->dev), net))
  1169. unregister_netdevice_queue(geneve->dev, &list);
  1170. }
  1171. /* unregister the devices gathered above */
  1172. unregister_netdevice_many(&list);
  1173. rtnl_unlock();
  1174. }
  1175. static struct pernet_operations geneve_net_ops = {
  1176. .init = geneve_init_net,
  1177. .exit = geneve_exit_net,
  1178. .id = &geneve_net_id,
  1179. .size = sizeof(struct geneve_net),
  1180. };
  1181. static int __init geneve_init_module(void)
  1182. {
  1183. int rc;
  1184. rc = register_pernet_subsys(&geneve_net_ops);
  1185. if (rc)
  1186. goto out1;
  1187. rc = rtnl_link_register(&geneve_link_ops);
  1188. if (rc)
  1189. goto out2;
  1190. return 0;
  1191. out2:
  1192. unregister_pernet_subsys(&geneve_net_ops);
  1193. out1:
  1194. return rc;
  1195. }
  1196. late_initcall(geneve_init_module);
  1197. static void __exit geneve_cleanup_module(void)
  1198. {
  1199. rtnl_link_unregister(&geneve_link_ops);
  1200. unregister_pernet_subsys(&geneve_net_ops);
  1201. }
  1202. module_exit(geneve_cleanup_module);
  1203. MODULE_LICENSE("GPL");
  1204. MODULE_VERSION(GENEVE_NETDEV_VER);
  1205. MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
  1206. MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
  1207. MODULE_ALIAS_RTNL_LINK("geneve");