vxlan.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /*
  2. * VXLAN: Virtual eXtensible Local Area Network
  3. *
  4. * Copyright (c) 2012-2013 Vyatta 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. * TODO
  11. * - IPv6 (not in RFC)
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rculist.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/in.h>
  23. #include <linux/ip.h>
  24. #include <linux/udp.h>
  25. #include <linux/igmp.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/hash.h>
  29. #include <linux/ethtool.h>
  30. #include <net/arp.h>
  31. #include <net/ndisc.h>
  32. #include <net/ip.h>
  33. #include <net/ip_tunnels.h>
  34. #include <net/icmp.h>
  35. #include <net/udp.h>
  36. #include <net/rtnetlink.h>
  37. #include <net/route.h>
  38. #include <net/dsfield.h>
  39. #include <net/inet_ecn.h>
  40. #include <net/net_namespace.h>
  41. #include <net/netns/generic.h>
  42. #define VXLAN_VERSION "0.1"
  43. #define PORT_HASH_BITS 8
  44. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  45. #define VNI_HASH_BITS 10
  46. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  47. #define FDB_HASH_BITS 8
  48. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  49. #define FDB_AGE_DEFAULT 300 /* 5 min */
  50. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  51. #define VXLAN_N_VID (1u << 24)
  52. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  53. /* IP header + UDP + VXLAN + Ethernet header */
  54. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  55. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  56. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  57. /* VXLAN protocol header */
  58. struct vxlanhdr {
  59. __be32 vx_flags;
  60. __be32 vx_vni;
  61. };
  62. /* UDP port for VXLAN traffic.
  63. * The IANA assigned port is 4789, but the Linux default is 8472
  64. * for compatibility with early adopters.
  65. */
  66. static unsigned short vxlan_port __read_mostly = 8472;
  67. module_param_named(udp_port, vxlan_port, ushort, 0444);
  68. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  69. static bool log_ecn_error = true;
  70. module_param(log_ecn_error, bool, 0644);
  71. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  72. static int vxlan_net_id;
  73. static const u8 all_zeros_mac[ETH_ALEN];
  74. /* per UDP socket information */
  75. struct vxlan_sock {
  76. struct hlist_node hlist;
  77. struct rcu_head rcu;
  78. struct work_struct del_work;
  79. atomic_t refcnt;
  80. struct socket *sock;
  81. struct hlist_head vni_list[VNI_HASH_SIZE];
  82. };
  83. /* per-network namespace private data for this module */
  84. struct vxlan_net {
  85. struct list_head vxlan_list;
  86. struct hlist_head sock_list[PORT_HASH_SIZE];
  87. spinlock_t sock_lock;
  88. };
  89. struct vxlan_rdst {
  90. __be32 remote_ip;
  91. __be16 remote_port;
  92. u32 remote_vni;
  93. u32 remote_ifindex;
  94. struct list_head list;
  95. struct rcu_head rcu;
  96. };
  97. /* Forwarding table entry */
  98. struct vxlan_fdb {
  99. struct hlist_node hlist; /* linked list of entries */
  100. struct rcu_head rcu;
  101. unsigned long updated; /* jiffies */
  102. unsigned long used;
  103. struct list_head remotes;
  104. u16 state; /* see ndm_state */
  105. u8 flags; /* see ndm_flags */
  106. u8 eth_addr[ETH_ALEN];
  107. };
  108. /* Pseudo network device */
  109. struct vxlan_dev {
  110. struct hlist_node hlist; /* vni hash table */
  111. struct list_head next; /* vxlan's per namespace list */
  112. struct vxlan_sock *vn_sock; /* listening socket */
  113. struct net_device *dev;
  114. struct vxlan_rdst default_dst; /* default destination */
  115. __be32 saddr; /* source address */
  116. __be16 dst_port;
  117. __u16 port_min; /* source port range */
  118. __u16 port_max;
  119. __u8 tos; /* TOS override */
  120. __u8 ttl;
  121. u32 flags; /* VXLAN_F_* below */
  122. struct work_struct sock_work;
  123. struct work_struct igmp_join;
  124. struct work_struct igmp_leave;
  125. unsigned long age_interval;
  126. struct timer_list age_timer;
  127. spinlock_t hash_lock;
  128. unsigned int addrcnt;
  129. unsigned int addrmax;
  130. struct hlist_head fdb_head[FDB_HASH_SIZE];
  131. };
  132. #define VXLAN_F_LEARN 0x01
  133. #define VXLAN_F_PROXY 0x02
  134. #define VXLAN_F_RSC 0x04
  135. #define VXLAN_F_L2MISS 0x08
  136. #define VXLAN_F_L3MISS 0x10
  137. /* salt for hash table */
  138. static u32 vxlan_salt __read_mostly;
  139. static struct workqueue_struct *vxlan_wq;
  140. static void vxlan_sock_work(struct work_struct *work);
  141. /* Virtual Network hash table head */
  142. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
  143. {
  144. return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
  145. }
  146. /* Socket hash table head */
  147. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  148. {
  149. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  150. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  151. }
  152. /* First remote destination for a forwarding entry.
  153. * Guaranteed to be non-NULL because remotes are never deleted.
  154. */
  155. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  156. {
  157. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  158. }
  159. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  160. {
  161. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  162. }
  163. /* Find VXLAN socket based on network namespace and UDP port */
  164. static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
  165. {
  166. struct vxlan_sock *vs;
  167. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  168. if (inet_sk(vs->sock->sk)->inet_sport == port)
  169. return vs;
  170. }
  171. return NULL;
  172. }
  173. /* Look up VNI in a per net namespace table */
  174. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
  175. {
  176. struct vxlan_sock *vs;
  177. struct vxlan_dev *vxlan;
  178. vs = vxlan_find_sock(net, port);
  179. if (!vs)
  180. return NULL;
  181. hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
  182. if (vxlan->default_dst.remote_vni == id)
  183. return vxlan;
  184. }
  185. return NULL;
  186. }
  187. /* Fill in neighbour message in skbuff. */
  188. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  189. const struct vxlan_fdb *fdb,
  190. u32 portid, u32 seq, int type, unsigned int flags,
  191. const struct vxlan_rdst *rdst)
  192. {
  193. unsigned long now = jiffies;
  194. struct nda_cacheinfo ci;
  195. struct nlmsghdr *nlh;
  196. struct ndmsg *ndm;
  197. bool send_ip, send_eth;
  198. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  199. if (nlh == NULL)
  200. return -EMSGSIZE;
  201. ndm = nlmsg_data(nlh);
  202. memset(ndm, 0, sizeof(*ndm));
  203. send_eth = send_ip = true;
  204. if (type == RTM_GETNEIGH) {
  205. ndm->ndm_family = AF_INET;
  206. send_ip = rdst->remote_ip != htonl(INADDR_ANY);
  207. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  208. } else
  209. ndm->ndm_family = AF_BRIDGE;
  210. ndm->ndm_state = fdb->state;
  211. ndm->ndm_ifindex = vxlan->dev->ifindex;
  212. ndm->ndm_flags = fdb->flags;
  213. ndm->ndm_type = NDA_DST;
  214. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  215. goto nla_put_failure;
  216. if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
  217. goto nla_put_failure;
  218. if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
  219. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  220. goto nla_put_failure;
  221. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  222. nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
  223. goto nla_put_failure;
  224. if (rdst->remote_ifindex &&
  225. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  226. goto nla_put_failure;
  227. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  228. ci.ndm_confirmed = 0;
  229. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  230. ci.ndm_refcnt = 0;
  231. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  232. goto nla_put_failure;
  233. return nlmsg_end(skb, nlh);
  234. nla_put_failure:
  235. nlmsg_cancel(skb, nlh);
  236. return -EMSGSIZE;
  237. }
  238. static inline size_t vxlan_nlmsg_size(void)
  239. {
  240. return NLMSG_ALIGN(sizeof(struct ndmsg))
  241. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  242. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  243. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  244. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  245. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  246. + nla_total_size(sizeof(struct nda_cacheinfo));
  247. }
  248. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  249. struct vxlan_fdb *fdb, int type)
  250. {
  251. struct net *net = dev_net(vxlan->dev);
  252. struct sk_buff *skb;
  253. int err = -ENOBUFS;
  254. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  255. if (skb == NULL)
  256. goto errout;
  257. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
  258. first_remote_rtnl(fdb));
  259. if (err < 0) {
  260. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  261. WARN_ON(err == -EMSGSIZE);
  262. kfree_skb(skb);
  263. goto errout;
  264. }
  265. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  266. return;
  267. errout:
  268. if (err < 0)
  269. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  270. }
  271. static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
  272. {
  273. struct vxlan_dev *vxlan = netdev_priv(dev);
  274. struct vxlan_fdb f = {
  275. .state = NUD_STALE,
  276. };
  277. struct vxlan_rdst remote = {
  278. .remote_ip = ipa, /* goes to NDA_DST */
  279. .remote_vni = VXLAN_N_VID,
  280. };
  281. INIT_LIST_HEAD(&f.remotes);
  282. list_add_rcu(&remote.list, &f.remotes);
  283. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  284. }
  285. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  286. {
  287. struct vxlan_fdb f = {
  288. .state = NUD_STALE,
  289. };
  290. INIT_LIST_HEAD(&f.remotes);
  291. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  292. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  293. }
  294. /* Hash Ethernet address */
  295. static u32 eth_hash(const unsigned char *addr)
  296. {
  297. u64 value = get_unaligned((u64 *)addr);
  298. /* only want 6 bytes */
  299. #ifdef __BIG_ENDIAN
  300. value >>= 16;
  301. #else
  302. value <<= 16;
  303. #endif
  304. return hash_64(value, FDB_HASH_BITS);
  305. }
  306. /* Hash chain to use given mac address */
  307. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  308. const u8 *mac)
  309. {
  310. return &vxlan->fdb_head[eth_hash(mac)];
  311. }
  312. /* Look up Ethernet address in forwarding table */
  313. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  314. const u8 *mac)
  315. {
  316. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  317. struct vxlan_fdb *f;
  318. hlist_for_each_entry_rcu(f, head, hlist) {
  319. if (compare_ether_addr(mac, f->eth_addr) == 0)
  320. return f;
  321. }
  322. return NULL;
  323. }
  324. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  325. const u8 *mac)
  326. {
  327. struct vxlan_fdb *f;
  328. f = __vxlan_find_mac(vxlan, mac);
  329. if (f)
  330. f->used = jiffies;
  331. return f;
  332. }
  333. /* caller should hold vxlan->hash_lock */
  334. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  335. __be32 ip, __be16 port,
  336. __u32 vni, __u32 ifindex)
  337. {
  338. struct vxlan_rdst *rd;
  339. list_for_each_entry(rd, &f->remotes, list) {
  340. if (rd->remote_ip == ip &&
  341. rd->remote_port == port &&
  342. rd->remote_vni == vni &&
  343. rd->remote_ifindex == ifindex)
  344. return rd;
  345. }
  346. return NULL;
  347. }
  348. /* Replace destination of unicast mac */
  349. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  350. __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
  351. {
  352. struct vxlan_rdst *rd;
  353. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  354. if (rd)
  355. return 0;
  356. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  357. if (!rd)
  358. return 0;
  359. rd->remote_ip = ip;
  360. rd->remote_port = port;
  361. rd->remote_vni = vni;
  362. rd->remote_ifindex = ifindex;
  363. return 1;
  364. }
  365. /* Add/update destinations for multicast */
  366. static int vxlan_fdb_append(struct vxlan_fdb *f,
  367. __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
  368. {
  369. struct vxlan_rdst *rd;
  370. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  371. if (rd)
  372. return 0;
  373. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  374. if (rd == NULL)
  375. return -ENOBUFS;
  376. rd->remote_ip = ip;
  377. rd->remote_port = port;
  378. rd->remote_vni = vni;
  379. rd->remote_ifindex = ifindex;
  380. list_add_tail_rcu(&rd->list, &f->remotes);
  381. return 1;
  382. }
  383. /* Add new entry to forwarding table -- assumes lock held */
  384. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  385. const u8 *mac, __be32 ip,
  386. __u16 state, __u16 flags,
  387. __be16 port, __u32 vni, __u32 ifindex,
  388. __u8 ndm_flags)
  389. {
  390. struct vxlan_fdb *f;
  391. int notify = 0;
  392. f = __vxlan_find_mac(vxlan, mac);
  393. if (f) {
  394. if (flags & NLM_F_EXCL) {
  395. netdev_dbg(vxlan->dev,
  396. "lost race to create %pM\n", mac);
  397. return -EEXIST;
  398. }
  399. if (f->state != state) {
  400. f->state = state;
  401. f->updated = jiffies;
  402. notify = 1;
  403. }
  404. if (f->flags != ndm_flags) {
  405. f->flags = ndm_flags;
  406. f->updated = jiffies;
  407. notify = 1;
  408. }
  409. if ((flags & NLM_F_REPLACE)) {
  410. /* Only change unicasts */
  411. if (!(is_multicast_ether_addr(f->eth_addr) ||
  412. is_zero_ether_addr(f->eth_addr))) {
  413. int rc = vxlan_fdb_replace(f, ip, port, vni,
  414. ifindex);
  415. if (rc < 0)
  416. return rc;
  417. notify |= rc;
  418. } else
  419. return -EOPNOTSUPP;
  420. }
  421. if ((flags & NLM_F_APPEND) &&
  422. (is_multicast_ether_addr(f->eth_addr) ||
  423. is_zero_ether_addr(f->eth_addr))) {
  424. int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
  425. if (rc < 0)
  426. return rc;
  427. notify |= rc;
  428. }
  429. } else {
  430. if (!(flags & NLM_F_CREATE))
  431. return -ENOENT;
  432. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  433. return -ENOSPC;
  434. /* Disallow replace to add a multicast entry */
  435. if ((flags & NLM_F_REPLACE) &&
  436. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  437. return -EOPNOTSUPP;
  438. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  439. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  440. if (!f)
  441. return -ENOMEM;
  442. notify = 1;
  443. f->state = state;
  444. f->flags = ndm_flags;
  445. f->updated = f->used = jiffies;
  446. INIT_LIST_HEAD(&f->remotes);
  447. memcpy(f->eth_addr, mac, ETH_ALEN);
  448. vxlan_fdb_append(f, ip, port, vni, ifindex);
  449. ++vxlan->addrcnt;
  450. hlist_add_head_rcu(&f->hlist,
  451. vxlan_fdb_head(vxlan, mac));
  452. }
  453. if (notify)
  454. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  455. return 0;
  456. }
  457. static void vxlan_fdb_free_rdst(struct rcu_head *head)
  458. {
  459. struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
  460. kfree(rd);
  461. }
  462. static void vxlan_fdb_free(struct rcu_head *head)
  463. {
  464. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  465. struct vxlan_rdst *rd, *nd;
  466. list_for_each_entry_safe(rd, nd, &f->remotes, list)
  467. kfree(rd);
  468. kfree(f);
  469. }
  470. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  471. {
  472. netdev_dbg(vxlan->dev,
  473. "delete %pM\n", f->eth_addr);
  474. --vxlan->addrcnt;
  475. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  476. hlist_del_rcu(&f->hlist);
  477. call_rcu(&f->rcu, vxlan_fdb_free);
  478. }
  479. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  480. __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
  481. {
  482. struct net *net = dev_net(vxlan->dev);
  483. if (tb[NDA_DST]) {
  484. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  485. return -EAFNOSUPPORT;
  486. *ip = nla_get_be32(tb[NDA_DST]);
  487. } else {
  488. *ip = htonl(INADDR_ANY);
  489. }
  490. if (tb[NDA_PORT]) {
  491. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  492. return -EINVAL;
  493. *port = nla_get_be16(tb[NDA_PORT]);
  494. } else {
  495. *port = vxlan->dst_port;
  496. }
  497. if (tb[NDA_VNI]) {
  498. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  499. return -EINVAL;
  500. *vni = nla_get_u32(tb[NDA_VNI]);
  501. } else {
  502. *vni = vxlan->default_dst.remote_vni;
  503. }
  504. if (tb[NDA_IFINDEX]) {
  505. struct net_device *tdev;
  506. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  507. return -EINVAL;
  508. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  509. tdev = dev_get_by_index(net, *ifindex);
  510. if (!tdev)
  511. return -EADDRNOTAVAIL;
  512. dev_put(tdev);
  513. } else {
  514. *ifindex = 0;
  515. }
  516. return 0;
  517. }
  518. /* Add static entry (via netlink) */
  519. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  520. struct net_device *dev,
  521. const unsigned char *addr, u16 flags)
  522. {
  523. struct vxlan_dev *vxlan = netdev_priv(dev);
  524. /* struct net *net = dev_net(vxlan->dev); */
  525. __be32 ip;
  526. __be16 port;
  527. u32 vni, ifindex;
  528. int err;
  529. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  530. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  531. ndm->ndm_state);
  532. return -EINVAL;
  533. }
  534. if (tb[NDA_DST] == NULL)
  535. return -EINVAL;
  536. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  537. if (err)
  538. return err;
  539. spin_lock_bh(&vxlan->hash_lock);
  540. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
  541. port, vni, ifindex, ndm->ndm_flags);
  542. spin_unlock_bh(&vxlan->hash_lock);
  543. return err;
  544. }
  545. /* Delete entry (via netlink) */
  546. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  547. struct net_device *dev,
  548. const unsigned char *addr)
  549. {
  550. struct vxlan_dev *vxlan = netdev_priv(dev);
  551. struct vxlan_fdb *f;
  552. struct vxlan_rdst *rd = NULL;
  553. __be32 ip;
  554. __be16 port;
  555. u32 vni, ifindex;
  556. int err;
  557. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  558. if (err)
  559. return err;
  560. err = -ENOENT;
  561. spin_lock_bh(&vxlan->hash_lock);
  562. f = vxlan_find_mac(vxlan, addr);
  563. if (!f)
  564. goto out;
  565. if (ip != htonl(INADDR_ANY)) {
  566. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  567. if (!rd)
  568. goto out;
  569. }
  570. err = 0;
  571. /* remove a destination if it's not the only one on the list,
  572. * otherwise destroy the fdb entry
  573. */
  574. if (rd && !list_is_singular(&f->remotes)) {
  575. list_del_rcu(&rd->list);
  576. call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
  577. goto out;
  578. }
  579. vxlan_fdb_destroy(vxlan, f);
  580. out:
  581. spin_unlock_bh(&vxlan->hash_lock);
  582. return err;
  583. }
  584. /* Dump forwarding table */
  585. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  586. struct net_device *dev, int idx)
  587. {
  588. struct vxlan_dev *vxlan = netdev_priv(dev);
  589. unsigned int h;
  590. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  591. struct vxlan_fdb *f;
  592. int err;
  593. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  594. struct vxlan_rdst *rd;
  595. if (idx < cb->args[0])
  596. goto skip;
  597. list_for_each_entry_rcu(rd, &f->remotes, list) {
  598. err = vxlan_fdb_info(skb, vxlan, f,
  599. NETLINK_CB(cb->skb).portid,
  600. cb->nlh->nlmsg_seq,
  601. RTM_NEWNEIGH,
  602. NLM_F_MULTI, rd);
  603. if (err < 0)
  604. goto out;
  605. }
  606. skip:
  607. ++idx;
  608. }
  609. }
  610. out:
  611. return idx;
  612. }
  613. /* Watch incoming packets to learn mapping between Ethernet address
  614. * and Tunnel endpoint.
  615. * Return true if packet is bogus and should be droppped.
  616. */
  617. static bool vxlan_snoop(struct net_device *dev,
  618. __be32 src_ip, const u8 *src_mac)
  619. {
  620. struct vxlan_dev *vxlan = netdev_priv(dev);
  621. struct vxlan_fdb *f;
  622. f = vxlan_find_mac(vxlan, src_mac);
  623. if (likely(f)) {
  624. struct vxlan_rdst *rdst = first_remote_rcu(f);
  625. if (likely(rdst->remote_ip == src_ip))
  626. return false;
  627. /* Don't migrate static entries, drop packets */
  628. if (f->state & NUD_NOARP)
  629. return true;
  630. if (net_ratelimit())
  631. netdev_info(dev,
  632. "%pM migrated from %pI4 to %pI4\n",
  633. src_mac, &rdst->remote_ip, &src_ip);
  634. rdst->remote_ip = src_ip;
  635. f->updated = jiffies;
  636. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  637. } else {
  638. /* learned new entry */
  639. spin_lock(&vxlan->hash_lock);
  640. /* close off race between vxlan_flush and incoming packets */
  641. if (netif_running(dev))
  642. vxlan_fdb_create(vxlan, src_mac, src_ip,
  643. NUD_REACHABLE,
  644. NLM_F_EXCL|NLM_F_CREATE,
  645. vxlan->dst_port,
  646. vxlan->default_dst.remote_vni,
  647. 0, NTF_SELF);
  648. spin_unlock(&vxlan->hash_lock);
  649. }
  650. return false;
  651. }
  652. /* See if multicast group is already in use by other ID */
  653. static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
  654. {
  655. struct vxlan_dev *vxlan;
  656. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  657. if (!netif_running(vxlan->dev))
  658. continue;
  659. if (vxlan->default_dst.remote_ip == remote_ip)
  660. return true;
  661. }
  662. return false;
  663. }
  664. static void vxlan_sock_hold(struct vxlan_sock *vs)
  665. {
  666. atomic_inc(&vs->refcnt);
  667. }
  668. static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
  669. {
  670. if (!atomic_dec_and_test(&vs->refcnt))
  671. return;
  672. spin_lock(&vn->sock_lock);
  673. hlist_del_rcu(&vs->hlist);
  674. spin_unlock(&vn->sock_lock);
  675. queue_work(vxlan_wq, &vs->del_work);
  676. }
  677. /* Callback to update multicast group membership when first VNI on
  678. * multicast asddress is brought up
  679. * Done as workqueue because ip_mc_join_group acquires RTNL.
  680. */
  681. static void vxlan_igmp_join(struct work_struct *work)
  682. {
  683. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  684. struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
  685. struct vxlan_sock *vs = vxlan->vn_sock;
  686. struct sock *sk = vs->sock->sk;
  687. struct ip_mreqn mreq = {
  688. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  689. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  690. };
  691. lock_sock(sk);
  692. ip_mc_join_group(sk, &mreq);
  693. release_sock(sk);
  694. vxlan_sock_release(vn, vs);
  695. dev_put(vxlan->dev);
  696. }
  697. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  698. static void vxlan_igmp_leave(struct work_struct *work)
  699. {
  700. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  701. struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
  702. struct vxlan_sock *vs = vxlan->vn_sock;
  703. struct sock *sk = vs->sock->sk;
  704. struct ip_mreqn mreq = {
  705. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  706. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  707. };
  708. lock_sock(sk);
  709. ip_mc_leave_group(sk, &mreq);
  710. release_sock(sk);
  711. vxlan_sock_release(vn, vs);
  712. dev_put(vxlan->dev);
  713. }
  714. /* Callback from net/ipv4/udp.c to receive packets */
  715. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  716. {
  717. struct iphdr *oip;
  718. struct vxlanhdr *vxh;
  719. struct vxlan_dev *vxlan;
  720. struct pcpu_tstats *stats;
  721. __be16 port;
  722. __u32 vni;
  723. int err;
  724. /* Need Vxlan and inner Ethernet header to be present */
  725. if (!pskb_may_pull(skb, VXLAN_HLEN))
  726. goto error;
  727. /* Return packets with reserved bits set */
  728. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  729. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  730. (vxh->vx_vni & htonl(0xff))) {
  731. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  732. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  733. goto error;
  734. }
  735. /* Is this VNI defined? */
  736. vni = ntohl(vxh->vx_vni) >> 8;
  737. port = inet_sk(sk)->inet_sport;
  738. vxlan = vxlan_find_vni(sock_net(sk), vni, port);
  739. if (!vxlan) {
  740. netdev_dbg(skb->dev, "unknown vni %d port %u\n",
  741. vni, ntohs(port));
  742. goto drop;
  743. }
  744. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) {
  745. vxlan->dev->stats.rx_length_errors++;
  746. vxlan->dev->stats.rx_errors++;
  747. goto drop;
  748. }
  749. skb_reset_mac_header(skb);
  750. skb->protocol = eth_type_trans(skb, vxlan->dev);
  751. /* Ignore packet loops (and multicast echo) */
  752. if (compare_ether_addr(eth_hdr(skb)->h_source,
  753. vxlan->dev->dev_addr) == 0)
  754. goto drop;
  755. /* Re-examine inner Ethernet packet */
  756. oip = ip_hdr(skb);
  757. if ((vxlan->flags & VXLAN_F_LEARN) &&
  758. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
  759. goto drop;
  760. skb_reset_network_header(skb);
  761. /* If the NIC driver gave us an encapsulated packet with
  762. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  763. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  764. * for us. Otherwise force the upper layers to verify it.
  765. */
  766. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  767. !(vxlan->dev->features & NETIF_F_RXCSUM))
  768. skb->ip_summed = CHECKSUM_NONE;
  769. skb->encapsulation = 0;
  770. err = IP_ECN_decapsulate(oip, skb);
  771. if (unlikely(err)) {
  772. if (log_ecn_error)
  773. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  774. &oip->saddr, oip->tos);
  775. if (err > 1) {
  776. ++vxlan->dev->stats.rx_frame_errors;
  777. ++vxlan->dev->stats.rx_errors;
  778. goto drop;
  779. }
  780. }
  781. stats = this_cpu_ptr(vxlan->dev->tstats);
  782. u64_stats_update_begin(&stats->syncp);
  783. stats->rx_packets++;
  784. stats->rx_bytes += skb->len;
  785. u64_stats_update_end(&stats->syncp);
  786. netif_rx(skb);
  787. return 0;
  788. error:
  789. return 1;
  790. drop:
  791. /* Consume bad packet */
  792. kfree_skb(skb);
  793. return 0;
  794. }
  795. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  796. {
  797. struct vxlan_dev *vxlan = netdev_priv(dev);
  798. struct arphdr *parp;
  799. u8 *arpptr, *sha;
  800. __be32 sip, tip;
  801. struct neighbour *n;
  802. if (dev->flags & IFF_NOARP)
  803. goto out;
  804. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  805. dev->stats.tx_dropped++;
  806. goto out;
  807. }
  808. parp = arp_hdr(skb);
  809. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  810. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  811. parp->ar_pro != htons(ETH_P_IP) ||
  812. parp->ar_op != htons(ARPOP_REQUEST) ||
  813. parp->ar_hln != dev->addr_len ||
  814. parp->ar_pln != 4)
  815. goto out;
  816. arpptr = (u8 *)parp + sizeof(struct arphdr);
  817. sha = arpptr;
  818. arpptr += dev->addr_len; /* sha */
  819. memcpy(&sip, arpptr, sizeof(sip));
  820. arpptr += sizeof(sip);
  821. arpptr += dev->addr_len; /* tha */
  822. memcpy(&tip, arpptr, sizeof(tip));
  823. if (ipv4_is_loopback(tip) ||
  824. ipv4_is_multicast(tip))
  825. goto out;
  826. n = neigh_lookup(&arp_tbl, &tip, dev);
  827. if (n) {
  828. struct vxlan_fdb *f;
  829. struct sk_buff *reply;
  830. if (!(n->nud_state & NUD_CONNECTED)) {
  831. neigh_release(n);
  832. goto out;
  833. }
  834. f = vxlan_find_mac(vxlan, n->ha);
  835. if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
  836. /* bridge-local neighbor */
  837. neigh_release(n);
  838. goto out;
  839. }
  840. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  841. n->ha, sha);
  842. neigh_release(n);
  843. skb_reset_mac_header(reply);
  844. __skb_pull(reply, skb_network_offset(reply));
  845. reply->ip_summed = CHECKSUM_UNNECESSARY;
  846. reply->pkt_type = PACKET_HOST;
  847. if (netif_rx_ni(reply) == NET_RX_DROP)
  848. dev->stats.rx_dropped++;
  849. } else if (vxlan->flags & VXLAN_F_L3MISS)
  850. vxlan_ip_miss(dev, tip);
  851. out:
  852. consume_skb(skb);
  853. return NETDEV_TX_OK;
  854. }
  855. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  856. {
  857. struct vxlan_dev *vxlan = netdev_priv(dev);
  858. struct neighbour *n;
  859. struct iphdr *pip;
  860. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  861. return false;
  862. n = NULL;
  863. switch (ntohs(eth_hdr(skb)->h_proto)) {
  864. case ETH_P_IP:
  865. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  866. return false;
  867. pip = ip_hdr(skb);
  868. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  869. break;
  870. default:
  871. return false;
  872. }
  873. if (n) {
  874. bool diff;
  875. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  876. if (diff) {
  877. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  878. dev->addr_len);
  879. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  880. }
  881. neigh_release(n);
  882. return diff;
  883. } else if (vxlan->flags & VXLAN_F_L3MISS)
  884. vxlan_ip_miss(dev, pip->daddr);
  885. return false;
  886. }
  887. static void vxlan_sock_put(struct sk_buff *skb)
  888. {
  889. sock_put(skb->sk);
  890. }
  891. /* On transmit, associate with the tunnel socket */
  892. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  893. {
  894. struct vxlan_dev *vxlan = netdev_priv(dev);
  895. struct sock *sk = vxlan->vn_sock->sock->sk;
  896. skb_orphan(skb);
  897. sock_hold(sk);
  898. skb->sk = sk;
  899. skb->destructor = vxlan_sock_put;
  900. }
  901. /* Compute source port for outgoing packet
  902. * first choice to use L4 flow hash since it will spread
  903. * better and maybe available from hardware
  904. * secondary choice is to use jhash on the Ethernet header
  905. */
  906. static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  907. {
  908. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  909. u32 hash;
  910. hash = skb_get_rxhash(skb);
  911. if (!hash)
  912. hash = jhash(skb->data, 2 * ETH_ALEN,
  913. (__force u32) skb->protocol);
  914. return htons((((u64) hash * range) >> 32) + vxlan->port_min);
  915. }
  916. static int handle_offloads(struct sk_buff *skb)
  917. {
  918. if (skb_is_gso(skb)) {
  919. int err = skb_unclone(skb, GFP_ATOMIC);
  920. if (unlikely(err))
  921. return err;
  922. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  923. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  924. skb->ip_summed = CHECKSUM_NONE;
  925. return 0;
  926. }
  927. /* Bypass encapsulation if the destination is local */
  928. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  929. struct vxlan_dev *dst_vxlan)
  930. {
  931. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  932. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  933. skb->pkt_type = PACKET_HOST;
  934. skb->encapsulation = 0;
  935. skb->dev = dst_vxlan->dev;
  936. __skb_pull(skb, skb_network_offset(skb));
  937. if (dst_vxlan->flags & VXLAN_F_LEARN)
  938. vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
  939. eth_hdr(skb)->h_source);
  940. u64_stats_update_begin(&tx_stats->syncp);
  941. tx_stats->tx_packets++;
  942. tx_stats->tx_bytes += skb->len;
  943. u64_stats_update_end(&tx_stats->syncp);
  944. if (netif_rx(skb) == NET_RX_SUCCESS) {
  945. u64_stats_update_begin(&rx_stats->syncp);
  946. rx_stats->rx_packets++;
  947. rx_stats->rx_bytes += skb->len;
  948. u64_stats_update_end(&rx_stats->syncp);
  949. } else {
  950. skb->dev->stats.rx_dropped++;
  951. }
  952. }
  953. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  954. struct vxlan_rdst *rdst, bool did_rsc)
  955. {
  956. struct vxlan_dev *vxlan = netdev_priv(dev);
  957. struct rtable *rt;
  958. const struct iphdr *old_iph;
  959. struct vxlanhdr *vxh;
  960. struct udphdr *uh;
  961. struct flowi4 fl4;
  962. __be32 dst;
  963. __be16 src_port, dst_port;
  964. u32 vni;
  965. __be16 df = 0;
  966. __u8 tos, ttl;
  967. int err;
  968. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  969. vni = rdst->remote_vni;
  970. dst = rdst->remote_ip;
  971. if (!dst) {
  972. if (did_rsc) {
  973. /* short-circuited back to local bridge */
  974. vxlan_encap_bypass(skb, vxlan, vxlan);
  975. return;
  976. }
  977. goto drop;
  978. }
  979. if (!skb->encapsulation) {
  980. skb_reset_inner_headers(skb);
  981. skb->encapsulation = 1;
  982. }
  983. /* Need space for new headers (invalidates iph ptr) */
  984. if (skb_cow_head(skb, VXLAN_HEADROOM))
  985. goto drop;
  986. old_iph = ip_hdr(skb);
  987. ttl = vxlan->ttl;
  988. if (!ttl && IN_MULTICAST(ntohl(dst)))
  989. ttl = 1;
  990. tos = vxlan->tos;
  991. if (tos == 1)
  992. tos = ip_tunnel_get_dsfield(old_iph, skb);
  993. src_port = vxlan_src_port(vxlan, skb);
  994. memset(&fl4, 0, sizeof(fl4));
  995. fl4.flowi4_oif = rdst->remote_ifindex;
  996. fl4.flowi4_tos = RT_TOS(tos);
  997. fl4.daddr = dst;
  998. fl4.saddr = vxlan->saddr;
  999. rt = ip_route_output_key(dev_net(dev), &fl4);
  1000. if (IS_ERR(rt)) {
  1001. netdev_dbg(dev, "no route to %pI4\n", &dst);
  1002. dev->stats.tx_carrier_errors++;
  1003. goto tx_error;
  1004. }
  1005. if (rt->dst.dev == dev) {
  1006. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  1007. ip_rt_put(rt);
  1008. dev->stats.collisions++;
  1009. goto tx_error;
  1010. }
  1011. /* Bypass encapsulation if the destination is local */
  1012. if (rt->rt_flags & RTCF_LOCAL &&
  1013. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1014. struct vxlan_dev *dst_vxlan;
  1015. ip_rt_put(rt);
  1016. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1017. if (!dst_vxlan)
  1018. goto tx_error;
  1019. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1020. return;
  1021. }
  1022. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1023. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1024. vxh->vx_vni = htonl(vni << 8);
  1025. __skb_push(skb, sizeof(*uh));
  1026. skb_reset_transport_header(skb);
  1027. uh = udp_hdr(skb);
  1028. uh->dest = dst_port;
  1029. uh->source = src_port;
  1030. uh->len = htons(skb->len);
  1031. uh->check = 0;
  1032. vxlan_set_owner(dev, skb);
  1033. if (handle_offloads(skb))
  1034. goto drop;
  1035. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1036. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1037. err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
  1038. IPPROTO_UDP, tos, ttl, df);
  1039. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1040. return;
  1041. drop:
  1042. dev->stats.tx_dropped++;
  1043. goto tx_free;
  1044. tx_error:
  1045. dev->stats.tx_errors++;
  1046. tx_free:
  1047. dev_kfree_skb(skb);
  1048. }
  1049. /* Transmit local packets over Vxlan
  1050. *
  1051. * Outer IP header inherits ECN and DF from inner header.
  1052. * Outer UDP destination is the VXLAN assigned port.
  1053. * source port is based on hash of flow
  1054. */
  1055. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1056. {
  1057. struct vxlan_dev *vxlan = netdev_priv(dev);
  1058. struct ethhdr *eth;
  1059. bool did_rsc = false;
  1060. struct vxlan_rdst *rdst;
  1061. struct vxlan_fdb *f;
  1062. skb_reset_mac_header(skb);
  1063. eth = eth_hdr(skb);
  1064. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  1065. return arp_reduce(dev, skb);
  1066. f = vxlan_find_mac(vxlan, eth->h_dest);
  1067. did_rsc = false;
  1068. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1069. ntohs(eth->h_proto) == ETH_P_IP) {
  1070. did_rsc = route_shortcircuit(dev, skb);
  1071. if (did_rsc)
  1072. f = vxlan_find_mac(vxlan, eth->h_dest);
  1073. }
  1074. if (f == NULL) {
  1075. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1076. if (f == NULL) {
  1077. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1078. !is_multicast_ether_addr(eth->h_dest))
  1079. vxlan_fdb_miss(vxlan, eth->h_dest);
  1080. dev->stats.tx_dropped++;
  1081. dev_kfree_skb(skb);
  1082. return NETDEV_TX_OK;
  1083. }
  1084. }
  1085. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1086. struct sk_buff *skb1;
  1087. skb1 = skb_clone(skb, GFP_ATOMIC);
  1088. if (skb1)
  1089. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1090. }
  1091. dev_kfree_skb(skb);
  1092. return NETDEV_TX_OK;
  1093. }
  1094. /* Walk the forwarding table and purge stale entries */
  1095. static void vxlan_cleanup(unsigned long arg)
  1096. {
  1097. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1098. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1099. unsigned int h;
  1100. if (!netif_running(vxlan->dev))
  1101. return;
  1102. spin_lock_bh(&vxlan->hash_lock);
  1103. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1104. struct hlist_node *p, *n;
  1105. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1106. struct vxlan_fdb *f
  1107. = container_of(p, struct vxlan_fdb, hlist);
  1108. unsigned long timeout;
  1109. if (f->state & NUD_PERMANENT)
  1110. continue;
  1111. timeout = f->used + vxlan->age_interval * HZ;
  1112. if (time_before_eq(timeout, jiffies)) {
  1113. netdev_dbg(vxlan->dev,
  1114. "garbage collect %pM\n",
  1115. f->eth_addr);
  1116. f->state = NUD_STALE;
  1117. vxlan_fdb_destroy(vxlan, f);
  1118. } else if (time_before(timeout, next_timer))
  1119. next_timer = timeout;
  1120. }
  1121. }
  1122. spin_unlock_bh(&vxlan->hash_lock);
  1123. mod_timer(&vxlan->age_timer, next_timer);
  1124. }
  1125. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1126. {
  1127. __u32 vni = vxlan->default_dst.remote_vni;
  1128. vxlan->vn_sock = vs;
  1129. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1130. }
  1131. /* Setup stats when device is created */
  1132. static int vxlan_init(struct net_device *dev)
  1133. {
  1134. struct vxlan_dev *vxlan = netdev_priv(dev);
  1135. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1136. struct vxlan_sock *vs;
  1137. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1138. if (!dev->tstats)
  1139. return -ENOMEM;
  1140. spin_lock(&vn->sock_lock);
  1141. vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
  1142. if (vs) {
  1143. /* If we have a socket with same port already, reuse it */
  1144. atomic_inc(&vs->refcnt);
  1145. vxlan_vs_add_dev(vs, vxlan);
  1146. } else {
  1147. /* otherwise make new socket outside of RTNL */
  1148. dev_hold(dev);
  1149. queue_work(vxlan_wq, &vxlan->sock_work);
  1150. }
  1151. spin_unlock(&vn->sock_lock);
  1152. return 0;
  1153. }
  1154. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1155. {
  1156. struct vxlan_fdb *f;
  1157. spin_lock_bh(&vxlan->hash_lock);
  1158. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1159. if (f)
  1160. vxlan_fdb_destroy(vxlan, f);
  1161. spin_unlock_bh(&vxlan->hash_lock);
  1162. }
  1163. static void vxlan_uninit(struct net_device *dev)
  1164. {
  1165. struct vxlan_dev *vxlan = netdev_priv(dev);
  1166. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1167. struct vxlan_sock *vs = vxlan->vn_sock;
  1168. vxlan_fdb_delete_default(vxlan);
  1169. if (vs)
  1170. vxlan_sock_release(vn, vs);
  1171. free_percpu(dev->tstats);
  1172. }
  1173. /* Start ageing timer and join group when device is brought up */
  1174. static int vxlan_open(struct net_device *dev)
  1175. {
  1176. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1177. struct vxlan_dev *vxlan = netdev_priv(dev);
  1178. struct vxlan_sock *vs = vxlan->vn_sock;
  1179. /* socket hasn't been created */
  1180. if (!vs)
  1181. return -ENOTCONN;
  1182. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
  1183. vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
  1184. vxlan_sock_hold(vs);
  1185. dev_hold(dev);
  1186. queue_work(vxlan_wq, &vxlan->igmp_join);
  1187. }
  1188. if (vxlan->age_interval)
  1189. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1190. return 0;
  1191. }
  1192. /* Purge the forwarding table */
  1193. static void vxlan_flush(struct vxlan_dev *vxlan)
  1194. {
  1195. unsigned int h;
  1196. spin_lock_bh(&vxlan->hash_lock);
  1197. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1198. struct hlist_node *p, *n;
  1199. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1200. struct vxlan_fdb *f
  1201. = container_of(p, struct vxlan_fdb, hlist);
  1202. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1203. if (!is_zero_ether_addr(f->eth_addr))
  1204. vxlan_fdb_destroy(vxlan, f);
  1205. }
  1206. }
  1207. spin_unlock_bh(&vxlan->hash_lock);
  1208. }
  1209. /* Cleanup timer and forwarding table on shutdown */
  1210. static int vxlan_stop(struct net_device *dev)
  1211. {
  1212. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1213. struct vxlan_dev *vxlan = netdev_priv(dev);
  1214. struct vxlan_sock *vs = vxlan->vn_sock;
  1215. if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
  1216. ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
  1217. vxlan_sock_hold(vs);
  1218. dev_hold(dev);
  1219. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1220. }
  1221. del_timer_sync(&vxlan->age_timer);
  1222. vxlan_flush(vxlan);
  1223. return 0;
  1224. }
  1225. /* Stub, nothing needs to be done. */
  1226. static void vxlan_set_multicast_list(struct net_device *dev)
  1227. {
  1228. }
  1229. static const struct net_device_ops vxlan_netdev_ops = {
  1230. .ndo_init = vxlan_init,
  1231. .ndo_uninit = vxlan_uninit,
  1232. .ndo_open = vxlan_open,
  1233. .ndo_stop = vxlan_stop,
  1234. .ndo_start_xmit = vxlan_xmit,
  1235. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1236. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1237. .ndo_change_mtu = eth_change_mtu,
  1238. .ndo_validate_addr = eth_validate_addr,
  1239. .ndo_set_mac_address = eth_mac_addr,
  1240. .ndo_fdb_add = vxlan_fdb_add,
  1241. .ndo_fdb_del = vxlan_fdb_delete,
  1242. .ndo_fdb_dump = vxlan_fdb_dump,
  1243. };
  1244. /* Info for udev, that this is a virtual tunnel endpoint */
  1245. static struct device_type vxlan_type = {
  1246. .name = "vxlan",
  1247. };
  1248. /* Initialize the device structure. */
  1249. static void vxlan_setup(struct net_device *dev)
  1250. {
  1251. struct vxlan_dev *vxlan = netdev_priv(dev);
  1252. unsigned int h;
  1253. int low, high;
  1254. eth_hw_addr_random(dev);
  1255. ether_setup(dev);
  1256. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1257. dev->netdev_ops = &vxlan_netdev_ops;
  1258. dev->destructor = free_netdev;
  1259. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1260. dev->tx_queue_len = 0;
  1261. dev->features |= NETIF_F_LLTX;
  1262. dev->features |= NETIF_F_NETNS_LOCAL;
  1263. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1264. dev->features |= NETIF_F_RXCSUM;
  1265. dev->features |= NETIF_F_GSO_SOFTWARE;
  1266. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1267. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1268. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1269. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1270. INIT_LIST_HEAD(&vxlan->next);
  1271. spin_lock_init(&vxlan->hash_lock);
  1272. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1273. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1274. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1275. init_timer_deferrable(&vxlan->age_timer);
  1276. vxlan->age_timer.function = vxlan_cleanup;
  1277. vxlan->age_timer.data = (unsigned long) vxlan;
  1278. inet_get_local_port_range(&low, &high);
  1279. vxlan->port_min = low;
  1280. vxlan->port_max = high;
  1281. vxlan->dst_port = htons(vxlan_port);
  1282. vxlan->dev = dev;
  1283. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1284. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1285. }
  1286. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1287. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1288. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1289. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1290. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1291. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1292. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1293. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1294. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1295. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1296. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1297. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1298. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1299. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1300. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1301. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1302. };
  1303. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1304. {
  1305. if (tb[IFLA_ADDRESS]) {
  1306. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1307. pr_debug("invalid link address (not ethernet)\n");
  1308. return -EINVAL;
  1309. }
  1310. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1311. pr_debug("invalid all zero ethernet address\n");
  1312. return -EADDRNOTAVAIL;
  1313. }
  1314. }
  1315. if (!data)
  1316. return -EINVAL;
  1317. if (data[IFLA_VXLAN_ID]) {
  1318. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1319. if (id >= VXLAN_VID_MASK)
  1320. return -ERANGE;
  1321. }
  1322. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1323. const struct ifla_vxlan_port_range *p
  1324. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1325. if (ntohs(p->high) < ntohs(p->low)) {
  1326. pr_debug("port range %u .. %u not valid\n",
  1327. ntohs(p->low), ntohs(p->high));
  1328. return -EINVAL;
  1329. }
  1330. }
  1331. return 0;
  1332. }
  1333. static void vxlan_get_drvinfo(struct net_device *netdev,
  1334. struct ethtool_drvinfo *drvinfo)
  1335. {
  1336. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1337. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1338. }
  1339. static const struct ethtool_ops vxlan_ethtool_ops = {
  1340. .get_drvinfo = vxlan_get_drvinfo,
  1341. .get_link = ethtool_op_get_link,
  1342. };
  1343. static void vxlan_del_work(struct work_struct *work)
  1344. {
  1345. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1346. sk_release_kernel(vs->sock->sk);
  1347. kfree_rcu(vs, rcu);
  1348. }
  1349. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
  1350. {
  1351. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1352. struct vxlan_sock *vs;
  1353. struct sock *sk;
  1354. struct sockaddr_in vxlan_addr = {
  1355. .sin_family = AF_INET,
  1356. .sin_addr.s_addr = htonl(INADDR_ANY),
  1357. .sin_port = port,
  1358. };
  1359. int rc;
  1360. unsigned int h;
  1361. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1362. if (!vs) {
  1363. pr_debug("memory alocation failure\n");
  1364. return ERR_PTR(-ENOMEM);
  1365. }
  1366. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1367. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1368. INIT_WORK(&vs->del_work, vxlan_del_work);
  1369. /* Create UDP socket for encapsulation receive. */
  1370. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
  1371. if (rc < 0) {
  1372. pr_debug("UDP socket create failed\n");
  1373. kfree(vs);
  1374. return ERR_PTR(rc);
  1375. }
  1376. /* Put in proper namespace */
  1377. sk = vs->sock->sk;
  1378. sk_change_net(sk, net);
  1379. rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
  1380. sizeof(vxlan_addr));
  1381. if (rc < 0) {
  1382. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1383. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1384. sk_release_kernel(sk);
  1385. kfree(vs);
  1386. return ERR_PTR(rc);
  1387. }
  1388. atomic_set(&vs->refcnt, 1);
  1389. /* Disable multicast loopback */
  1390. inet_sk(sk)->mc_loop = 0;
  1391. spin_lock(&vn->sock_lock);
  1392. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  1393. spin_unlock(&vn->sock_lock);
  1394. /* Mark socket as an encapsulation socket. */
  1395. udp_sk(sk)->encap_type = 1;
  1396. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1397. udp_encap_enable();
  1398. return vs;
  1399. }
  1400. static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port)
  1401. {
  1402. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1403. struct vxlan_sock *vs;
  1404. vs = vxlan_socket_create(net, port);
  1405. if (!IS_ERR(vs))
  1406. return vs;
  1407. spin_lock(&vn->sock_lock);
  1408. vs = vxlan_find_sock(net, port);
  1409. if (vs)
  1410. atomic_inc(&vs->refcnt);
  1411. else
  1412. vs = ERR_PTR(-EINVAL);
  1413. spin_unlock(&vn->sock_lock);
  1414. return vs;
  1415. }
  1416. /* Scheduled at device creation to bind to a socket */
  1417. static void vxlan_sock_work(struct work_struct *work)
  1418. {
  1419. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  1420. struct net *net = dev_net(vxlan->dev);
  1421. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1422. __be16 port = vxlan->dst_port;
  1423. struct vxlan_sock *nvs;
  1424. nvs = vxlan_sock_add(net, port);
  1425. spin_lock(&vn->sock_lock);
  1426. if (!IS_ERR(nvs))
  1427. vxlan_vs_add_dev(nvs, vxlan);
  1428. spin_unlock(&vn->sock_lock);
  1429. dev_put(vxlan->dev);
  1430. }
  1431. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1432. struct nlattr *tb[], struct nlattr *data[])
  1433. {
  1434. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1435. struct vxlan_dev *vxlan = netdev_priv(dev);
  1436. struct vxlan_rdst *dst = &vxlan->default_dst;
  1437. __u32 vni;
  1438. int err;
  1439. if (!data[IFLA_VXLAN_ID])
  1440. return -EINVAL;
  1441. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1442. dst->remote_vni = vni;
  1443. if (data[IFLA_VXLAN_GROUP])
  1444. dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1445. if (data[IFLA_VXLAN_LOCAL])
  1446. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1447. if (data[IFLA_VXLAN_LINK] &&
  1448. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1449. struct net_device *lowerdev
  1450. = __dev_get_by_index(net, dst->remote_ifindex);
  1451. if (!lowerdev) {
  1452. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  1453. return -ENODEV;
  1454. }
  1455. if (!tb[IFLA_MTU])
  1456. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1457. /* update header length based on lower device */
  1458. dev->hard_header_len = lowerdev->hard_header_len +
  1459. VXLAN_HEADROOM;
  1460. }
  1461. if (data[IFLA_VXLAN_TOS])
  1462. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1463. if (data[IFLA_VXLAN_TTL])
  1464. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1465. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1466. vxlan->flags |= VXLAN_F_LEARN;
  1467. if (data[IFLA_VXLAN_AGEING])
  1468. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1469. else
  1470. vxlan->age_interval = FDB_AGE_DEFAULT;
  1471. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1472. vxlan->flags |= VXLAN_F_PROXY;
  1473. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1474. vxlan->flags |= VXLAN_F_RSC;
  1475. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1476. vxlan->flags |= VXLAN_F_L2MISS;
  1477. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1478. vxlan->flags |= VXLAN_F_L3MISS;
  1479. if (data[IFLA_VXLAN_LIMIT])
  1480. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1481. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1482. const struct ifla_vxlan_port_range *p
  1483. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1484. vxlan->port_min = ntohs(p->low);
  1485. vxlan->port_max = ntohs(p->high);
  1486. }
  1487. if (data[IFLA_VXLAN_PORT])
  1488. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  1489. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  1490. pr_info("duplicate VNI %u\n", vni);
  1491. return -EEXIST;
  1492. }
  1493. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  1494. /* create an fdb entry for default destination */
  1495. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  1496. vxlan->default_dst.remote_ip,
  1497. NUD_REACHABLE|NUD_PERMANENT,
  1498. NLM_F_EXCL|NLM_F_CREATE,
  1499. vxlan->dst_port, vxlan->default_dst.remote_vni,
  1500. vxlan->default_dst.remote_ifindex, NTF_SELF);
  1501. if (err)
  1502. return err;
  1503. err = register_netdevice(dev);
  1504. if (err) {
  1505. vxlan_fdb_delete_default(vxlan);
  1506. return err;
  1507. }
  1508. list_add(&vxlan->next, &vn->vxlan_list);
  1509. return 0;
  1510. }
  1511. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1512. {
  1513. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1514. struct vxlan_dev *vxlan = netdev_priv(dev);
  1515. spin_lock(&vn->sock_lock);
  1516. if (!hlist_unhashed(&vxlan->hlist))
  1517. hlist_del_rcu(&vxlan->hlist);
  1518. spin_unlock(&vn->sock_lock);
  1519. list_del(&vxlan->next);
  1520. unregister_netdevice_queue(dev, head);
  1521. }
  1522. static size_t vxlan_get_size(const struct net_device *dev)
  1523. {
  1524. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1525. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1526. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1527. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1528. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1529. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1530. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1531. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1532. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1533. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1534. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1535. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1536. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1537. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1538. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  1539. 0;
  1540. }
  1541. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1542. {
  1543. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1544. const struct vxlan_rdst *dst = &vxlan->default_dst;
  1545. struct ifla_vxlan_port_range ports = {
  1546. .low = htons(vxlan->port_min),
  1547. .high = htons(vxlan->port_max),
  1548. };
  1549. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  1550. goto nla_put_failure;
  1551. if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
  1552. goto nla_put_failure;
  1553. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  1554. goto nla_put_failure;
  1555. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1556. goto nla_put_failure;
  1557. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1558. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1559. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1560. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1561. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1562. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1563. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1564. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1565. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1566. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1567. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1568. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1569. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  1570. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  1571. goto nla_put_failure;
  1572. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1573. goto nla_put_failure;
  1574. return 0;
  1575. nla_put_failure:
  1576. return -EMSGSIZE;
  1577. }
  1578. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1579. .kind = "vxlan",
  1580. .maxtype = IFLA_VXLAN_MAX,
  1581. .policy = vxlan_policy,
  1582. .priv_size = sizeof(struct vxlan_dev),
  1583. .setup = vxlan_setup,
  1584. .validate = vxlan_validate,
  1585. .newlink = vxlan_newlink,
  1586. .dellink = vxlan_dellink,
  1587. .get_size = vxlan_get_size,
  1588. .fill_info = vxlan_fill_info,
  1589. };
  1590. static __net_init int vxlan_init_net(struct net *net)
  1591. {
  1592. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1593. unsigned int h;
  1594. INIT_LIST_HEAD(&vn->vxlan_list);
  1595. spin_lock_init(&vn->sock_lock);
  1596. for (h = 0; h < PORT_HASH_SIZE; ++h)
  1597. INIT_HLIST_HEAD(&vn->sock_list[h]);
  1598. return 0;
  1599. }
  1600. static __net_exit void vxlan_exit_net(struct net *net)
  1601. {
  1602. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1603. struct vxlan_dev *vxlan;
  1604. LIST_HEAD(list);
  1605. rtnl_lock();
  1606. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  1607. unregister_netdevice_queue(vxlan->dev, &list);
  1608. unregister_netdevice_many(&list);
  1609. rtnl_unlock();
  1610. }
  1611. static struct pernet_operations vxlan_net_ops = {
  1612. .init = vxlan_init_net,
  1613. .exit = vxlan_exit_net,
  1614. .id = &vxlan_net_id,
  1615. .size = sizeof(struct vxlan_net),
  1616. };
  1617. static int __init vxlan_init_module(void)
  1618. {
  1619. int rc;
  1620. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  1621. if (!vxlan_wq)
  1622. return -ENOMEM;
  1623. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1624. rc = register_pernet_device(&vxlan_net_ops);
  1625. if (rc)
  1626. goto out1;
  1627. rc = rtnl_link_register(&vxlan_link_ops);
  1628. if (rc)
  1629. goto out2;
  1630. return 0;
  1631. out2:
  1632. unregister_pernet_device(&vxlan_net_ops);
  1633. out1:
  1634. destroy_workqueue(vxlan_wq);
  1635. return rc;
  1636. }
  1637. late_initcall(vxlan_init_module);
  1638. static void __exit vxlan_cleanup_module(void)
  1639. {
  1640. rtnl_link_unregister(&vxlan_link_ops);
  1641. destroy_workqueue(vxlan_wq);
  1642. unregister_pernet_device(&vxlan_net_ops);
  1643. rcu_barrier();
  1644. }
  1645. module_exit(vxlan_cleanup_module);
  1646. MODULE_LICENSE("GPL");
  1647. MODULE_VERSION(VXLAN_VERSION);
  1648. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  1649. MODULE_ALIAS_RTNL_LINK("vxlan");