af_mpls.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netconf.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/percpu.h>
  13. #include <net/ip.h>
  14. #include <net/dst.h>
  15. #include <net/sock.h>
  16. #include <net/arp.h>
  17. #include <net/ip_fib.h>
  18. #include <net/netevent.h>
  19. #include <net/netns/generic.h>
  20. #if IS_ENABLED(CONFIG_IPV6)
  21. #include <net/ipv6.h>
  22. #endif
  23. #include <net/addrconf.h>
  24. #include <net/nexthop.h>
  25. #include "internal.h"
  26. /* Maximum number of labels to look ahead at when selecting a path of
  27. * a multipath route
  28. */
  29. #define MAX_MP_SELECT_LABELS 4
  30. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  31. static int zero = 0;
  32. static int one = 1;
  33. static int label_limit = (1 << 20) - 1;
  34. static int ttl_max = 255;
  35. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  36. struct nlmsghdr *nlh, struct net *net, u32 portid,
  37. unsigned int nlm_flags);
  38. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  39. {
  40. struct mpls_route *rt = NULL;
  41. if (index < net->mpls.platform_labels) {
  42. struct mpls_route __rcu **platform_label =
  43. rcu_dereference(net->mpls.platform_label);
  44. rt = rcu_dereference(platform_label[index]);
  45. }
  46. return rt;
  47. }
  48. bool mpls_output_possible(const struct net_device *dev)
  49. {
  50. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  51. }
  52. EXPORT_SYMBOL_GPL(mpls_output_possible);
  53. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  54. {
  55. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  56. int nh_index = nh - rt->rt_nh;
  57. return nh0_via + rt->rt_max_alen * nh_index;
  58. }
  59. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  60. const struct mpls_nh *nh)
  61. {
  62. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  63. }
  64. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  65. {
  66. /* The size of the layer 2.5 labels to be added for this route */
  67. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  68. }
  69. unsigned int mpls_dev_mtu(const struct net_device *dev)
  70. {
  71. /* The amount of data the layer 2 frame can hold */
  72. return dev->mtu;
  73. }
  74. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  75. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  76. {
  77. if (skb->len <= mtu)
  78. return false;
  79. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  84. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  85. const struct sk_buff *skb)
  86. {
  87. struct mpls_dev *mdev;
  88. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  89. mdev = mpls_dev_get(dev);
  90. if (mdev)
  91. MPLS_INC_STATS_LEN(mdev, skb->len,
  92. tx_packets,
  93. tx_bytes);
  94. } else if (skb->protocol == htons(ETH_P_IP)) {
  95. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  96. #if IS_ENABLED(CONFIG_IPV6)
  97. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  98. struct inet6_dev *in6dev = __in6_dev_get(dev);
  99. if (in6dev)
  100. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  101. IPSTATS_MIB_OUT, skb->len);
  102. #endif
  103. }
  104. }
  105. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  106. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  107. {
  108. struct mpls_entry_decoded dec;
  109. unsigned int mpls_hdr_len = 0;
  110. struct mpls_shim_hdr *hdr;
  111. bool eli_seen = false;
  112. int label_index;
  113. u32 hash = 0;
  114. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  115. label_index++) {
  116. mpls_hdr_len += sizeof(*hdr);
  117. if (!pskb_may_pull(skb, mpls_hdr_len))
  118. break;
  119. /* Read and decode the current label */
  120. hdr = mpls_hdr(skb) + label_index;
  121. dec = mpls_entry_decode(hdr);
  122. /* RFC6790 - reserved labels MUST NOT be used as keys
  123. * for the load-balancing function
  124. */
  125. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  126. hash = jhash_1word(dec.label, hash);
  127. /* The entropy label follows the entropy label
  128. * indicator, so this means that the entropy
  129. * label was just added to the hash - no need to
  130. * go any deeper either in the label stack or in the
  131. * payload
  132. */
  133. if (eli_seen)
  134. break;
  135. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  136. eli_seen = true;
  137. }
  138. if (!dec.bos)
  139. continue;
  140. /* found bottom label; does skb have room for a header? */
  141. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  142. const struct iphdr *v4hdr;
  143. v4hdr = (const struct iphdr *)(hdr + 1);
  144. if (v4hdr->version == 4) {
  145. hash = jhash_3words(ntohl(v4hdr->saddr),
  146. ntohl(v4hdr->daddr),
  147. v4hdr->protocol, hash);
  148. } else if (v4hdr->version == 6 &&
  149. pskb_may_pull(skb, mpls_hdr_len +
  150. sizeof(struct ipv6hdr))) {
  151. const struct ipv6hdr *v6hdr;
  152. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  153. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  154. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  155. hash = jhash_1word(v6hdr->nexthdr, hash);
  156. }
  157. }
  158. break;
  159. }
  160. return hash;
  161. }
  162. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  163. struct sk_buff *skb)
  164. {
  165. int alive = ACCESS_ONCE(rt->rt_nhn_alive);
  166. u32 hash = 0;
  167. int nh_index = 0;
  168. int n = 0;
  169. /* No need to look further into packet if there's only
  170. * one path
  171. */
  172. if (rt->rt_nhn == 1)
  173. goto out;
  174. if (alive <= 0)
  175. return NULL;
  176. hash = mpls_multipath_hash(rt, skb);
  177. nh_index = hash % alive;
  178. if (alive == rt->rt_nhn)
  179. goto out;
  180. for_nexthops(rt) {
  181. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  182. continue;
  183. if (n == nh_index)
  184. return nh;
  185. n++;
  186. } endfor_nexthops(rt);
  187. out:
  188. return &rt->rt_nh[nh_index];
  189. }
  190. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  191. struct sk_buff *skb, struct mpls_entry_decoded dec)
  192. {
  193. enum mpls_payload_type payload_type;
  194. bool success = false;
  195. /* The IPv4 code below accesses through the IPv4 header
  196. * checksum, which is 12 bytes into the packet.
  197. * The IPv6 code below accesses through the IPv6 hop limit
  198. * which is 8 bytes into the packet.
  199. *
  200. * For all supported cases there should always be at least 12
  201. * bytes of packet data present. The IPv4 header is 20 bytes
  202. * without options and the IPv6 header is always 40 bytes
  203. * long.
  204. */
  205. if (!pskb_may_pull(skb, 12))
  206. return false;
  207. payload_type = rt->rt_payload_type;
  208. if (payload_type == MPT_UNSPEC)
  209. payload_type = ip_hdr(skb)->version;
  210. switch (payload_type) {
  211. case MPT_IPV4: {
  212. struct iphdr *hdr4 = ip_hdr(skb);
  213. u8 new_ttl;
  214. skb->protocol = htons(ETH_P_IP);
  215. /* If propagating TTL, take the decremented TTL from
  216. * the incoming MPLS header, otherwise decrement the
  217. * TTL, but only if not 0 to avoid underflow.
  218. */
  219. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  220. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  221. net->mpls.ip_ttl_propagate))
  222. new_ttl = dec.ttl;
  223. else
  224. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  225. csum_replace2(&hdr4->check,
  226. htons(hdr4->ttl << 8),
  227. htons(new_ttl << 8));
  228. hdr4->ttl = new_ttl;
  229. success = true;
  230. break;
  231. }
  232. case MPT_IPV6: {
  233. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  234. skb->protocol = htons(ETH_P_IPV6);
  235. /* If propagating TTL, take the decremented TTL from
  236. * the incoming MPLS header, otherwise decrement the
  237. * hop limit, but only if not 0 to avoid underflow.
  238. */
  239. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  240. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  241. net->mpls.ip_ttl_propagate))
  242. hdr6->hop_limit = dec.ttl;
  243. else if (hdr6->hop_limit)
  244. hdr6->hop_limit = hdr6->hop_limit - 1;
  245. success = true;
  246. break;
  247. }
  248. case MPT_UNSPEC:
  249. /* Should have decided which protocol it is by now */
  250. break;
  251. }
  252. return success;
  253. }
  254. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  255. struct packet_type *pt, struct net_device *orig_dev)
  256. {
  257. struct net *net = dev_net(dev);
  258. struct mpls_shim_hdr *hdr;
  259. struct mpls_route *rt;
  260. struct mpls_nh *nh;
  261. struct mpls_entry_decoded dec;
  262. struct net_device *out_dev;
  263. struct mpls_dev *out_mdev;
  264. struct mpls_dev *mdev;
  265. unsigned int hh_len;
  266. unsigned int new_header_size;
  267. unsigned int mtu;
  268. int err;
  269. /* Careful this entire function runs inside of an rcu critical section */
  270. mdev = mpls_dev_get(dev);
  271. if (!mdev)
  272. goto drop;
  273. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  274. rx_bytes);
  275. if (!mdev->input_enabled) {
  276. MPLS_INC_STATS(mdev, rx_dropped);
  277. goto drop;
  278. }
  279. if (skb->pkt_type != PACKET_HOST)
  280. goto err;
  281. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  282. goto err;
  283. if (!pskb_may_pull(skb, sizeof(*hdr)))
  284. goto err;
  285. /* Read and decode the label */
  286. hdr = mpls_hdr(skb);
  287. dec = mpls_entry_decode(hdr);
  288. rt = mpls_route_input_rcu(net, dec.label);
  289. if (!rt) {
  290. MPLS_INC_STATS(mdev, rx_noroute);
  291. goto drop;
  292. }
  293. nh = mpls_select_multipath(rt, skb);
  294. if (!nh)
  295. goto err;
  296. /* Pop the label */
  297. skb_pull(skb, sizeof(*hdr));
  298. skb_reset_network_header(skb);
  299. skb_orphan(skb);
  300. if (skb_warn_if_lro(skb))
  301. goto err;
  302. skb_forward_csum(skb);
  303. /* Verify ttl is valid */
  304. if (dec.ttl <= 1)
  305. goto err;
  306. dec.ttl -= 1;
  307. /* Find the output device */
  308. out_dev = rcu_dereference(nh->nh_dev);
  309. if (!mpls_output_possible(out_dev))
  310. goto tx_err;
  311. /* Verify the destination can hold the packet */
  312. new_header_size = mpls_nh_header_size(nh);
  313. mtu = mpls_dev_mtu(out_dev);
  314. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  315. goto tx_err;
  316. hh_len = LL_RESERVED_SPACE(out_dev);
  317. if (!out_dev->header_ops)
  318. hh_len = 0;
  319. /* Ensure there is enough space for the headers in the skb */
  320. if (skb_cow(skb, hh_len + new_header_size))
  321. goto tx_err;
  322. skb->dev = out_dev;
  323. skb->protocol = htons(ETH_P_MPLS_UC);
  324. if (unlikely(!new_header_size && dec.bos)) {
  325. /* Penultimate hop popping */
  326. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  327. goto err;
  328. } else {
  329. bool bos;
  330. int i;
  331. skb_push(skb, new_header_size);
  332. skb_reset_network_header(skb);
  333. /* Push the new labels */
  334. hdr = mpls_hdr(skb);
  335. bos = dec.bos;
  336. for (i = nh->nh_labels - 1; i >= 0; i--) {
  337. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  338. dec.ttl, 0, bos);
  339. bos = false;
  340. }
  341. }
  342. mpls_stats_inc_outucastpkts(out_dev, skb);
  343. /* If via wasn't specified then send out using device address */
  344. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  345. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  346. out_dev->dev_addr, skb);
  347. else
  348. err = neigh_xmit(nh->nh_via_table, out_dev,
  349. mpls_nh_via(rt, nh), skb);
  350. if (err)
  351. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  352. __func__, err);
  353. return 0;
  354. tx_err:
  355. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  356. if (out_mdev)
  357. MPLS_INC_STATS(out_mdev, tx_errors);
  358. goto drop;
  359. err:
  360. MPLS_INC_STATS(mdev, rx_errors);
  361. drop:
  362. kfree_skb(skb);
  363. return NET_RX_DROP;
  364. }
  365. static struct packet_type mpls_packet_type __read_mostly = {
  366. .type = cpu_to_be16(ETH_P_MPLS_UC),
  367. .func = mpls_forward,
  368. };
  369. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  370. [RTA_DST] = { .type = NLA_U32 },
  371. [RTA_OIF] = { .type = NLA_U32 },
  372. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  373. };
  374. struct mpls_route_config {
  375. u32 rc_protocol;
  376. u32 rc_ifindex;
  377. u8 rc_via_table;
  378. u8 rc_via_alen;
  379. u8 rc_via[MAX_VIA_ALEN];
  380. u32 rc_label;
  381. u8 rc_ttl_propagate;
  382. u8 rc_output_labels;
  383. u32 rc_output_label[MAX_NEW_LABELS];
  384. u32 rc_nlflags;
  385. enum mpls_payload_type rc_payload_type;
  386. struct nl_info rc_nlinfo;
  387. struct rtnexthop *rc_mp;
  388. int rc_mp_len;
  389. };
  390. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  391. {
  392. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  393. struct mpls_route *rt;
  394. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  395. VIA_ALEN_ALIGN) +
  396. num_nh * max_alen_aligned,
  397. GFP_KERNEL);
  398. if (rt) {
  399. rt->rt_nhn = num_nh;
  400. rt->rt_nhn_alive = num_nh;
  401. rt->rt_max_alen = max_alen_aligned;
  402. }
  403. return rt;
  404. }
  405. static void mpls_rt_free(struct mpls_route *rt)
  406. {
  407. if (rt)
  408. kfree_rcu(rt, rt_rcu);
  409. }
  410. static void mpls_notify_route(struct net *net, unsigned index,
  411. struct mpls_route *old, struct mpls_route *new,
  412. const struct nl_info *info)
  413. {
  414. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  415. unsigned portid = info ? info->portid : 0;
  416. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  417. struct mpls_route *rt = new ? new : old;
  418. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  419. /* Ignore reserved labels for now */
  420. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  421. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  422. }
  423. static void mpls_route_update(struct net *net, unsigned index,
  424. struct mpls_route *new,
  425. const struct nl_info *info)
  426. {
  427. struct mpls_route __rcu **platform_label;
  428. struct mpls_route *rt;
  429. ASSERT_RTNL();
  430. platform_label = rtnl_dereference(net->mpls.platform_label);
  431. rt = rtnl_dereference(platform_label[index]);
  432. rcu_assign_pointer(platform_label[index], new);
  433. mpls_notify_route(net, index, rt, new, info);
  434. /* If we removed a route free it now */
  435. mpls_rt_free(rt);
  436. }
  437. static unsigned find_free_label(struct net *net)
  438. {
  439. struct mpls_route __rcu **platform_label;
  440. size_t platform_labels;
  441. unsigned index;
  442. platform_label = rtnl_dereference(net->mpls.platform_label);
  443. platform_labels = net->mpls.platform_labels;
  444. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  445. index++) {
  446. if (!rtnl_dereference(platform_label[index]))
  447. return index;
  448. }
  449. return LABEL_NOT_SPECIFIED;
  450. }
  451. #if IS_ENABLED(CONFIG_INET)
  452. static struct net_device *inet_fib_lookup_dev(struct net *net,
  453. const void *addr)
  454. {
  455. struct net_device *dev;
  456. struct rtable *rt;
  457. struct in_addr daddr;
  458. memcpy(&daddr, addr, sizeof(struct in_addr));
  459. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  460. if (IS_ERR(rt))
  461. return ERR_CAST(rt);
  462. dev = rt->dst.dev;
  463. dev_hold(dev);
  464. ip_rt_put(rt);
  465. return dev;
  466. }
  467. #else
  468. static struct net_device *inet_fib_lookup_dev(struct net *net,
  469. const void *addr)
  470. {
  471. return ERR_PTR(-EAFNOSUPPORT);
  472. }
  473. #endif
  474. #if IS_ENABLED(CONFIG_IPV6)
  475. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  476. const void *addr)
  477. {
  478. struct net_device *dev;
  479. struct dst_entry *dst;
  480. struct flowi6 fl6;
  481. int err;
  482. if (!ipv6_stub)
  483. return ERR_PTR(-EAFNOSUPPORT);
  484. memset(&fl6, 0, sizeof(fl6));
  485. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  486. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  487. if (err)
  488. return ERR_PTR(err);
  489. dev = dst->dev;
  490. dev_hold(dev);
  491. dst_release(dst);
  492. return dev;
  493. }
  494. #else
  495. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  496. const void *addr)
  497. {
  498. return ERR_PTR(-EAFNOSUPPORT);
  499. }
  500. #endif
  501. static struct net_device *find_outdev(struct net *net,
  502. struct mpls_route *rt,
  503. struct mpls_nh *nh, int oif)
  504. {
  505. struct net_device *dev = NULL;
  506. if (!oif) {
  507. switch (nh->nh_via_table) {
  508. case NEIGH_ARP_TABLE:
  509. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  510. break;
  511. case NEIGH_ND_TABLE:
  512. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  513. break;
  514. case NEIGH_LINK_TABLE:
  515. break;
  516. }
  517. } else {
  518. dev = dev_get_by_index(net, oif);
  519. }
  520. if (!dev)
  521. return ERR_PTR(-ENODEV);
  522. if (IS_ERR(dev))
  523. return dev;
  524. /* The caller is holding rtnl anyways, so release the dev reference */
  525. dev_put(dev);
  526. return dev;
  527. }
  528. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  529. struct mpls_nh *nh, int oif)
  530. {
  531. struct net_device *dev = NULL;
  532. int err = -ENODEV;
  533. dev = find_outdev(net, rt, nh, oif);
  534. if (IS_ERR(dev)) {
  535. err = PTR_ERR(dev);
  536. dev = NULL;
  537. goto errout;
  538. }
  539. /* Ensure this is a supported device */
  540. err = -EINVAL;
  541. if (!mpls_dev_get(dev))
  542. goto errout;
  543. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  544. (dev->addr_len != nh->nh_via_alen))
  545. goto errout;
  546. RCU_INIT_POINTER(nh->nh_dev, dev);
  547. if (!(dev->flags & IFF_UP)) {
  548. nh->nh_flags |= RTNH_F_DEAD;
  549. } else {
  550. unsigned int flags;
  551. flags = dev_get_flags(dev);
  552. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  553. nh->nh_flags |= RTNH_F_LINKDOWN;
  554. }
  555. return 0;
  556. errout:
  557. return err;
  558. }
  559. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  560. struct mpls_route *rt)
  561. {
  562. struct net *net = cfg->rc_nlinfo.nl_net;
  563. struct mpls_nh *nh = rt->rt_nh;
  564. int err;
  565. int i;
  566. if (!nh)
  567. return -ENOMEM;
  568. err = -EINVAL;
  569. /* Ensure only a supported number of labels are present */
  570. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  571. goto errout;
  572. nh->nh_labels = cfg->rc_output_labels;
  573. for (i = 0; i < nh->nh_labels; i++)
  574. nh->nh_label[i] = cfg->rc_output_label[i];
  575. nh->nh_via_table = cfg->rc_via_table;
  576. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  577. nh->nh_via_alen = cfg->rc_via_alen;
  578. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  579. if (err)
  580. goto errout;
  581. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  582. rt->rt_nhn_alive--;
  583. return 0;
  584. errout:
  585. return err;
  586. }
  587. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  588. struct mpls_nh *nh, int oif, struct nlattr *via,
  589. struct nlattr *newdst)
  590. {
  591. int err = -ENOMEM;
  592. if (!nh)
  593. goto errout;
  594. if (newdst) {
  595. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  596. &nh->nh_labels, nh->nh_label);
  597. if (err)
  598. goto errout;
  599. }
  600. if (via) {
  601. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  602. __mpls_nh_via(rt, nh));
  603. if (err)
  604. goto errout;
  605. } else {
  606. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  607. }
  608. err = mpls_nh_assign_dev(net, rt, nh, oif);
  609. if (err)
  610. goto errout;
  611. return 0;
  612. errout:
  613. return err;
  614. }
  615. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  616. u8 cfg_via_alen, u8 *max_via_alen)
  617. {
  618. int nhs = 0;
  619. int remaining = len;
  620. if (!rtnh) {
  621. *max_via_alen = cfg_via_alen;
  622. return 1;
  623. }
  624. *max_via_alen = 0;
  625. while (rtnh_ok(rtnh, remaining)) {
  626. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  627. int attrlen;
  628. attrlen = rtnh_attrlen(rtnh);
  629. nla = nla_find(attrs, attrlen, RTA_VIA);
  630. if (nla && nla_len(nla) >=
  631. offsetof(struct rtvia, rtvia_addr)) {
  632. int via_alen = nla_len(nla) -
  633. offsetof(struct rtvia, rtvia_addr);
  634. if (via_alen <= MAX_VIA_ALEN)
  635. *max_via_alen = max_t(u16, *max_via_alen,
  636. via_alen);
  637. }
  638. nhs++;
  639. rtnh = rtnh_next(rtnh, &remaining);
  640. }
  641. /* leftover implies invalid nexthop configuration, discard it */
  642. return remaining > 0 ? 0 : nhs;
  643. }
  644. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  645. struct mpls_route *rt)
  646. {
  647. struct rtnexthop *rtnh = cfg->rc_mp;
  648. struct nlattr *nla_via, *nla_newdst;
  649. int remaining = cfg->rc_mp_len;
  650. int nhs = 0;
  651. int err = 0;
  652. change_nexthops(rt) {
  653. int attrlen;
  654. nla_via = NULL;
  655. nla_newdst = NULL;
  656. err = -EINVAL;
  657. if (!rtnh_ok(rtnh, remaining))
  658. goto errout;
  659. /* neither weighted multipath nor any flags
  660. * are supported
  661. */
  662. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  663. goto errout;
  664. attrlen = rtnh_attrlen(rtnh);
  665. if (attrlen > 0) {
  666. struct nlattr *attrs = rtnh_attrs(rtnh);
  667. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  668. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  669. }
  670. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  671. rtnh->rtnh_ifindex, nla_via, nla_newdst);
  672. if (err)
  673. goto errout;
  674. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  675. rt->rt_nhn_alive--;
  676. rtnh = rtnh_next(rtnh, &remaining);
  677. nhs++;
  678. } endfor_nexthops(rt);
  679. rt->rt_nhn = nhs;
  680. return 0;
  681. errout:
  682. return err;
  683. }
  684. static int mpls_route_add(struct mpls_route_config *cfg)
  685. {
  686. struct mpls_route __rcu **platform_label;
  687. struct net *net = cfg->rc_nlinfo.nl_net;
  688. struct mpls_route *rt, *old;
  689. int err = -EINVAL;
  690. u8 max_via_alen;
  691. unsigned index;
  692. int nhs;
  693. index = cfg->rc_label;
  694. /* If a label was not specified during insert pick one */
  695. if ((index == LABEL_NOT_SPECIFIED) &&
  696. (cfg->rc_nlflags & NLM_F_CREATE)) {
  697. index = find_free_label(net);
  698. }
  699. /* Reserved labels may not be set */
  700. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  701. goto errout;
  702. /* The full 20 bit range may not be supported. */
  703. if (index >= net->mpls.platform_labels)
  704. goto errout;
  705. /* Append makes no sense with mpls */
  706. err = -EOPNOTSUPP;
  707. if (cfg->rc_nlflags & NLM_F_APPEND)
  708. goto errout;
  709. err = -EEXIST;
  710. platform_label = rtnl_dereference(net->mpls.platform_label);
  711. old = rtnl_dereference(platform_label[index]);
  712. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  713. goto errout;
  714. err = -EEXIST;
  715. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  716. goto errout;
  717. err = -ENOENT;
  718. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  719. goto errout;
  720. err = -EINVAL;
  721. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  722. cfg->rc_via_alen, &max_via_alen);
  723. if (nhs == 0)
  724. goto errout;
  725. err = -ENOMEM;
  726. rt = mpls_rt_alloc(nhs, max_via_alen);
  727. if (!rt)
  728. goto errout;
  729. rt->rt_protocol = cfg->rc_protocol;
  730. rt->rt_payload_type = cfg->rc_payload_type;
  731. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  732. if (cfg->rc_mp)
  733. err = mpls_nh_build_multi(cfg, rt);
  734. else
  735. err = mpls_nh_build_from_cfg(cfg, rt);
  736. if (err)
  737. goto freert;
  738. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  739. return 0;
  740. freert:
  741. mpls_rt_free(rt);
  742. errout:
  743. return err;
  744. }
  745. static int mpls_route_del(struct mpls_route_config *cfg)
  746. {
  747. struct net *net = cfg->rc_nlinfo.nl_net;
  748. unsigned index;
  749. int err = -EINVAL;
  750. index = cfg->rc_label;
  751. /* Reserved labels may not be removed */
  752. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  753. goto errout;
  754. /* The full 20 bit range may not be supported */
  755. if (index >= net->mpls.platform_labels)
  756. goto errout;
  757. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  758. err = 0;
  759. errout:
  760. return err;
  761. }
  762. static void mpls_get_stats(struct mpls_dev *mdev,
  763. struct mpls_link_stats *stats)
  764. {
  765. struct mpls_pcpu_stats *p;
  766. int i;
  767. memset(stats, 0, sizeof(*stats));
  768. for_each_possible_cpu(i) {
  769. struct mpls_link_stats local;
  770. unsigned int start;
  771. p = per_cpu_ptr(mdev->stats, i);
  772. do {
  773. start = u64_stats_fetch_begin(&p->syncp);
  774. local = p->stats;
  775. } while (u64_stats_fetch_retry(&p->syncp, start));
  776. stats->rx_packets += local.rx_packets;
  777. stats->rx_bytes += local.rx_bytes;
  778. stats->tx_packets += local.tx_packets;
  779. stats->tx_bytes += local.tx_bytes;
  780. stats->rx_errors += local.rx_errors;
  781. stats->tx_errors += local.tx_errors;
  782. stats->rx_dropped += local.rx_dropped;
  783. stats->tx_dropped += local.tx_dropped;
  784. stats->rx_noroute += local.rx_noroute;
  785. }
  786. }
  787. static int mpls_fill_stats_af(struct sk_buff *skb,
  788. const struct net_device *dev)
  789. {
  790. struct mpls_link_stats *stats;
  791. struct mpls_dev *mdev;
  792. struct nlattr *nla;
  793. mdev = mpls_dev_get(dev);
  794. if (!mdev)
  795. return -ENODATA;
  796. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  797. sizeof(struct mpls_link_stats),
  798. MPLS_STATS_UNSPEC);
  799. if (!nla)
  800. return -EMSGSIZE;
  801. stats = nla_data(nla);
  802. mpls_get_stats(mdev, stats);
  803. return 0;
  804. }
  805. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  806. {
  807. struct mpls_dev *mdev;
  808. mdev = mpls_dev_get(dev);
  809. if (!mdev)
  810. return 0;
  811. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  812. }
  813. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  814. u32 portid, u32 seq, int event,
  815. unsigned int flags, int type)
  816. {
  817. struct nlmsghdr *nlh;
  818. struct netconfmsg *ncm;
  819. bool all = false;
  820. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  821. flags);
  822. if (!nlh)
  823. return -EMSGSIZE;
  824. if (type == NETCONFA_ALL)
  825. all = true;
  826. ncm = nlmsg_data(nlh);
  827. ncm->ncm_family = AF_MPLS;
  828. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  829. goto nla_put_failure;
  830. if ((all || type == NETCONFA_INPUT) &&
  831. nla_put_s32(skb, NETCONFA_INPUT,
  832. mdev->input_enabled) < 0)
  833. goto nla_put_failure;
  834. nlmsg_end(skb, nlh);
  835. return 0;
  836. nla_put_failure:
  837. nlmsg_cancel(skb, nlh);
  838. return -EMSGSIZE;
  839. }
  840. static int mpls_netconf_msgsize_devconf(int type)
  841. {
  842. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  843. + nla_total_size(4); /* NETCONFA_IFINDEX */
  844. bool all = false;
  845. if (type == NETCONFA_ALL)
  846. all = true;
  847. if (all || type == NETCONFA_INPUT)
  848. size += nla_total_size(4);
  849. return size;
  850. }
  851. static void mpls_netconf_notify_devconf(struct net *net, int type,
  852. struct mpls_dev *mdev)
  853. {
  854. struct sk_buff *skb;
  855. int err = -ENOBUFS;
  856. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  857. if (!skb)
  858. goto errout;
  859. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, RTM_NEWNETCONF,
  860. 0, type);
  861. if (err < 0) {
  862. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  863. WARN_ON(err == -EMSGSIZE);
  864. kfree_skb(skb);
  865. goto errout;
  866. }
  867. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  868. return;
  869. errout:
  870. if (err < 0)
  871. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  872. }
  873. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  874. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  875. };
  876. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  877. struct nlmsghdr *nlh)
  878. {
  879. struct net *net = sock_net(in_skb->sk);
  880. struct nlattr *tb[NETCONFA_MAX + 1];
  881. struct netconfmsg *ncm;
  882. struct net_device *dev;
  883. struct mpls_dev *mdev;
  884. struct sk_buff *skb;
  885. int ifindex;
  886. int err;
  887. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  888. devconf_mpls_policy);
  889. if (err < 0)
  890. goto errout;
  891. err = -EINVAL;
  892. if (!tb[NETCONFA_IFINDEX])
  893. goto errout;
  894. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  895. dev = __dev_get_by_index(net, ifindex);
  896. if (!dev)
  897. goto errout;
  898. mdev = mpls_dev_get(dev);
  899. if (!mdev)
  900. goto errout;
  901. err = -ENOBUFS;
  902. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  903. if (!skb)
  904. goto errout;
  905. err = mpls_netconf_fill_devconf(skb, mdev,
  906. NETLINK_CB(in_skb).portid,
  907. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  908. NETCONFA_ALL);
  909. if (err < 0) {
  910. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  911. WARN_ON(err == -EMSGSIZE);
  912. kfree_skb(skb);
  913. goto errout;
  914. }
  915. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  916. errout:
  917. return err;
  918. }
  919. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  920. struct netlink_callback *cb)
  921. {
  922. struct net *net = sock_net(skb->sk);
  923. struct hlist_head *head;
  924. struct net_device *dev;
  925. struct mpls_dev *mdev;
  926. int idx, s_idx;
  927. int h, s_h;
  928. s_h = cb->args[0];
  929. s_idx = idx = cb->args[1];
  930. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  931. idx = 0;
  932. head = &net->dev_index_head[h];
  933. rcu_read_lock();
  934. cb->seq = net->dev_base_seq;
  935. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  936. if (idx < s_idx)
  937. goto cont;
  938. mdev = mpls_dev_get(dev);
  939. if (!mdev)
  940. goto cont;
  941. if (mpls_netconf_fill_devconf(skb, mdev,
  942. NETLINK_CB(cb->skb).portid,
  943. cb->nlh->nlmsg_seq,
  944. RTM_NEWNETCONF,
  945. NLM_F_MULTI,
  946. NETCONFA_ALL) < 0) {
  947. rcu_read_unlock();
  948. goto done;
  949. }
  950. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  951. cont:
  952. idx++;
  953. }
  954. rcu_read_unlock();
  955. }
  956. done:
  957. cb->args[0] = h;
  958. cb->args[1] = idx;
  959. return skb->len;
  960. }
  961. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  962. (&((struct mpls_dev *)0)->field)
  963. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  964. void __user *buffer,
  965. size_t *lenp, loff_t *ppos)
  966. {
  967. int oval = *(int *)ctl->data;
  968. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  969. if (write) {
  970. struct mpls_dev *mdev = ctl->extra1;
  971. int i = (int *)ctl->data - (int *)mdev;
  972. struct net *net = ctl->extra2;
  973. int val = *(int *)ctl->data;
  974. if (i == offsetof(struct mpls_dev, input_enabled) &&
  975. val != oval) {
  976. mpls_netconf_notify_devconf(net,
  977. NETCONFA_INPUT,
  978. mdev);
  979. }
  980. }
  981. return ret;
  982. }
  983. static const struct ctl_table mpls_dev_table[] = {
  984. {
  985. .procname = "input",
  986. .maxlen = sizeof(int),
  987. .mode = 0644,
  988. .proc_handler = mpls_conf_proc,
  989. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  990. },
  991. { }
  992. };
  993. static int mpls_dev_sysctl_register(struct net_device *dev,
  994. struct mpls_dev *mdev)
  995. {
  996. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  997. struct net *net = dev_net(dev);
  998. struct ctl_table *table;
  999. int i;
  1000. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1001. if (!table)
  1002. goto out;
  1003. /* Table data contains only offsets relative to the base of
  1004. * the mdev at this point, so make them absolute.
  1005. */
  1006. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  1007. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1008. table[i].extra1 = mdev;
  1009. table[i].extra2 = net;
  1010. }
  1011. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1012. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  1013. if (!mdev->sysctl)
  1014. goto free;
  1015. return 0;
  1016. free:
  1017. kfree(table);
  1018. out:
  1019. return -ENOBUFS;
  1020. }
  1021. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  1022. {
  1023. struct ctl_table *table;
  1024. table = mdev->sysctl->ctl_table_arg;
  1025. unregister_net_sysctl_table(mdev->sysctl);
  1026. kfree(table);
  1027. }
  1028. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1029. {
  1030. struct mpls_dev *mdev;
  1031. int err = -ENOMEM;
  1032. int i;
  1033. ASSERT_RTNL();
  1034. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1035. if (!mdev)
  1036. return ERR_PTR(err);
  1037. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1038. if (!mdev->stats)
  1039. goto free;
  1040. for_each_possible_cpu(i) {
  1041. struct mpls_pcpu_stats *mpls_stats;
  1042. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1043. u64_stats_init(&mpls_stats->syncp);
  1044. }
  1045. err = mpls_dev_sysctl_register(dev, mdev);
  1046. if (err)
  1047. goto free;
  1048. mdev->dev = dev;
  1049. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1050. return mdev;
  1051. free:
  1052. free_percpu(mdev->stats);
  1053. kfree(mdev);
  1054. return ERR_PTR(err);
  1055. }
  1056. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1057. {
  1058. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1059. free_percpu(mdev->stats);
  1060. kfree(mdev);
  1061. }
  1062. static void mpls_ifdown(struct net_device *dev, int event)
  1063. {
  1064. struct mpls_route __rcu **platform_label;
  1065. struct net *net = dev_net(dev);
  1066. unsigned int nh_flags = RTNH_F_DEAD | RTNH_F_LINKDOWN;
  1067. unsigned int alive, deleted;
  1068. unsigned index;
  1069. platform_label = rtnl_dereference(net->mpls.platform_label);
  1070. for (index = 0; index < net->mpls.platform_labels; index++) {
  1071. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1072. if (!rt)
  1073. continue;
  1074. alive = 0;
  1075. deleted = 0;
  1076. change_nexthops(rt) {
  1077. if (rtnl_dereference(nh->nh_dev) != dev)
  1078. goto next;
  1079. switch (event) {
  1080. case NETDEV_DOWN:
  1081. case NETDEV_UNREGISTER:
  1082. nh->nh_flags |= RTNH_F_DEAD;
  1083. /* fall through */
  1084. case NETDEV_CHANGE:
  1085. nh->nh_flags |= RTNH_F_LINKDOWN;
  1086. break;
  1087. }
  1088. if (event == NETDEV_UNREGISTER)
  1089. RCU_INIT_POINTER(nh->nh_dev, NULL);
  1090. next:
  1091. if (!(nh->nh_flags & nh_flags))
  1092. alive++;
  1093. if (!rtnl_dereference(nh->nh_dev))
  1094. deleted++;
  1095. } endfor_nexthops(rt);
  1096. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1097. /* if there are no more nexthops, delete the route */
  1098. if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
  1099. mpls_route_update(net, index, NULL, NULL);
  1100. }
  1101. }
  1102. static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
  1103. {
  1104. struct mpls_route __rcu **platform_label;
  1105. struct net *net = dev_net(dev);
  1106. unsigned index;
  1107. int alive;
  1108. platform_label = rtnl_dereference(net->mpls.platform_label);
  1109. for (index = 0; index < net->mpls.platform_labels; index++) {
  1110. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1111. if (!rt)
  1112. continue;
  1113. alive = 0;
  1114. change_nexthops(rt) {
  1115. struct net_device *nh_dev =
  1116. rtnl_dereference(nh->nh_dev);
  1117. if (!(nh->nh_flags & nh_flags)) {
  1118. alive++;
  1119. continue;
  1120. }
  1121. if (nh_dev != dev)
  1122. continue;
  1123. alive++;
  1124. nh->nh_flags &= ~nh_flags;
  1125. } endfor_nexthops(rt);
  1126. ACCESS_ONCE(rt->rt_nhn_alive) = alive;
  1127. }
  1128. }
  1129. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1130. void *ptr)
  1131. {
  1132. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1133. struct mpls_dev *mdev;
  1134. unsigned int flags;
  1135. if (event == NETDEV_REGISTER) {
  1136. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  1137. if (dev->type == ARPHRD_ETHER ||
  1138. dev->type == ARPHRD_LOOPBACK ||
  1139. dev->type == ARPHRD_IPGRE ||
  1140. dev->type == ARPHRD_SIT ||
  1141. dev->type == ARPHRD_TUNNEL) {
  1142. mdev = mpls_add_dev(dev);
  1143. if (IS_ERR(mdev))
  1144. return notifier_from_errno(PTR_ERR(mdev));
  1145. }
  1146. return NOTIFY_OK;
  1147. }
  1148. mdev = mpls_dev_get(dev);
  1149. if (!mdev)
  1150. return NOTIFY_OK;
  1151. switch (event) {
  1152. case NETDEV_DOWN:
  1153. mpls_ifdown(dev, event);
  1154. break;
  1155. case NETDEV_UP:
  1156. flags = dev_get_flags(dev);
  1157. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1158. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1159. else
  1160. mpls_ifup(dev, RTNH_F_DEAD);
  1161. break;
  1162. case NETDEV_CHANGE:
  1163. flags = dev_get_flags(dev);
  1164. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1165. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1166. else
  1167. mpls_ifdown(dev, event);
  1168. break;
  1169. case NETDEV_UNREGISTER:
  1170. mpls_ifdown(dev, event);
  1171. mdev = mpls_dev_get(dev);
  1172. if (mdev) {
  1173. mpls_dev_sysctl_unregister(mdev);
  1174. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1175. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1176. }
  1177. break;
  1178. case NETDEV_CHANGENAME:
  1179. mdev = mpls_dev_get(dev);
  1180. if (mdev) {
  1181. int err;
  1182. mpls_dev_sysctl_unregister(mdev);
  1183. err = mpls_dev_sysctl_register(dev, mdev);
  1184. if (err)
  1185. return notifier_from_errno(err);
  1186. }
  1187. break;
  1188. }
  1189. return NOTIFY_OK;
  1190. }
  1191. static struct notifier_block mpls_dev_notifier = {
  1192. .notifier_call = mpls_dev_notify,
  1193. };
  1194. static int nla_put_via(struct sk_buff *skb,
  1195. u8 table, const void *addr, int alen)
  1196. {
  1197. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1198. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1199. };
  1200. struct nlattr *nla;
  1201. struct rtvia *via;
  1202. int family = AF_UNSPEC;
  1203. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1204. if (!nla)
  1205. return -EMSGSIZE;
  1206. if (table <= NEIGH_NR_TABLES)
  1207. family = table_to_family[table];
  1208. via = nla_data(nla);
  1209. via->rtvia_family = family;
  1210. memcpy(via->rtvia_addr, addr, alen);
  1211. return 0;
  1212. }
  1213. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1214. u8 labels, const u32 label[])
  1215. {
  1216. struct nlattr *nla;
  1217. struct mpls_shim_hdr *nla_label;
  1218. bool bos;
  1219. int i;
  1220. nla = nla_reserve(skb, attrtype, labels*4);
  1221. if (!nla)
  1222. return -EMSGSIZE;
  1223. nla_label = nla_data(nla);
  1224. bos = true;
  1225. for (i = labels - 1; i >= 0; i--) {
  1226. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1227. bos = false;
  1228. }
  1229. return 0;
  1230. }
  1231. EXPORT_SYMBOL_GPL(nla_put_labels);
  1232. int nla_get_labels(const struct nlattr *nla,
  1233. u32 max_labels, u8 *labels, u32 label[])
  1234. {
  1235. unsigned len = nla_len(nla);
  1236. unsigned nla_labels;
  1237. struct mpls_shim_hdr *nla_label;
  1238. bool bos;
  1239. int i;
  1240. /* len needs to be an even multiple of 4 (the label size) */
  1241. if (len & 3)
  1242. return -EINVAL;
  1243. /* Limit the number of new labels allowed */
  1244. nla_labels = len/4;
  1245. if (nla_labels > max_labels)
  1246. return -EINVAL;
  1247. nla_label = nla_data(nla);
  1248. bos = true;
  1249. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1250. struct mpls_entry_decoded dec;
  1251. dec = mpls_entry_decode(nla_label + i);
  1252. /* Ensure the bottom of stack flag is properly set
  1253. * and ttl and tc are both clear.
  1254. */
  1255. if ((dec.bos != bos) || dec.ttl || dec.tc)
  1256. return -EINVAL;
  1257. switch (dec.label) {
  1258. case MPLS_LABEL_IMPLNULL:
  1259. /* RFC3032: This is a label that an LSR may
  1260. * assign and distribute, but which never
  1261. * actually appears in the encapsulation.
  1262. */
  1263. return -EINVAL;
  1264. }
  1265. label[i] = dec.label;
  1266. }
  1267. *labels = nla_labels;
  1268. return 0;
  1269. }
  1270. EXPORT_SYMBOL_GPL(nla_get_labels);
  1271. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  1272. u8 *via_table, u8 via_addr[])
  1273. {
  1274. struct rtvia *via = nla_data(nla);
  1275. int err = -EINVAL;
  1276. int alen;
  1277. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  1278. goto errout;
  1279. alen = nla_len(nla) -
  1280. offsetof(struct rtvia, rtvia_addr);
  1281. if (alen > MAX_VIA_ALEN)
  1282. goto errout;
  1283. /* Validate the address family */
  1284. switch (via->rtvia_family) {
  1285. case AF_PACKET:
  1286. *via_table = NEIGH_LINK_TABLE;
  1287. break;
  1288. case AF_INET:
  1289. *via_table = NEIGH_ARP_TABLE;
  1290. if (alen != 4)
  1291. goto errout;
  1292. break;
  1293. case AF_INET6:
  1294. *via_table = NEIGH_ND_TABLE;
  1295. if (alen != 16)
  1296. goto errout;
  1297. break;
  1298. default:
  1299. /* Unsupported address family */
  1300. goto errout;
  1301. }
  1302. memcpy(via_addr, via->rtvia_addr, alen);
  1303. *via_alen = alen;
  1304. err = 0;
  1305. errout:
  1306. return err;
  1307. }
  1308. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1309. struct mpls_route_config *cfg)
  1310. {
  1311. struct rtmsg *rtm;
  1312. struct nlattr *tb[RTA_MAX+1];
  1313. int index;
  1314. int err;
  1315. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  1316. if (err < 0)
  1317. goto errout;
  1318. err = -EINVAL;
  1319. rtm = nlmsg_data(nlh);
  1320. memset(cfg, 0, sizeof(*cfg));
  1321. if (rtm->rtm_family != AF_MPLS)
  1322. goto errout;
  1323. if (rtm->rtm_dst_len != 20)
  1324. goto errout;
  1325. if (rtm->rtm_src_len != 0)
  1326. goto errout;
  1327. if (rtm->rtm_tos != 0)
  1328. goto errout;
  1329. if (rtm->rtm_table != RT_TABLE_MAIN)
  1330. goto errout;
  1331. /* Any value is acceptable for rtm_protocol */
  1332. /* As mpls uses destination specific addresses
  1333. * (or source specific address in the case of multicast)
  1334. * all addresses have universal scope.
  1335. */
  1336. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1337. goto errout;
  1338. if (rtm->rtm_type != RTN_UNICAST)
  1339. goto errout;
  1340. if (rtm->rtm_flags != 0)
  1341. goto errout;
  1342. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1343. cfg->rc_protocol = rtm->rtm_protocol;
  1344. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1345. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1346. cfg->rc_nlflags = nlh->nlmsg_flags;
  1347. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1348. cfg->rc_nlinfo.nlh = nlh;
  1349. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1350. for (index = 0; index <= RTA_MAX; index++) {
  1351. struct nlattr *nla = tb[index];
  1352. if (!nla)
  1353. continue;
  1354. switch (index) {
  1355. case RTA_OIF:
  1356. cfg->rc_ifindex = nla_get_u32(nla);
  1357. break;
  1358. case RTA_NEWDST:
  1359. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1360. &cfg->rc_output_labels,
  1361. cfg->rc_output_label))
  1362. goto errout;
  1363. break;
  1364. case RTA_DST:
  1365. {
  1366. u8 label_count;
  1367. if (nla_get_labels(nla, 1, &label_count,
  1368. &cfg->rc_label))
  1369. goto errout;
  1370. /* Reserved labels may not be set */
  1371. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1372. goto errout;
  1373. break;
  1374. }
  1375. case RTA_VIA:
  1376. {
  1377. if (nla_get_via(nla, &cfg->rc_via_alen,
  1378. &cfg->rc_via_table, cfg->rc_via))
  1379. goto errout;
  1380. break;
  1381. }
  1382. case RTA_MULTIPATH:
  1383. {
  1384. cfg->rc_mp = nla_data(nla);
  1385. cfg->rc_mp_len = nla_len(nla);
  1386. break;
  1387. }
  1388. case RTA_TTL_PROPAGATE:
  1389. {
  1390. u8 ttl_propagate = nla_get_u8(nla);
  1391. if (ttl_propagate > 1)
  1392. goto errout;
  1393. cfg->rc_ttl_propagate = ttl_propagate ?
  1394. MPLS_TTL_PROP_ENABLED :
  1395. MPLS_TTL_PROP_DISABLED;
  1396. break;
  1397. }
  1398. default:
  1399. /* Unsupported attribute */
  1400. goto errout;
  1401. }
  1402. }
  1403. err = 0;
  1404. errout:
  1405. return err;
  1406. }
  1407. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1408. {
  1409. struct mpls_route_config cfg;
  1410. int err;
  1411. err = rtm_to_route_config(skb, nlh, &cfg);
  1412. if (err < 0)
  1413. return err;
  1414. return mpls_route_del(&cfg);
  1415. }
  1416. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1417. {
  1418. struct mpls_route_config cfg;
  1419. int err;
  1420. err = rtm_to_route_config(skb, nlh, &cfg);
  1421. if (err < 0)
  1422. return err;
  1423. return mpls_route_add(&cfg);
  1424. }
  1425. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1426. u32 label, struct mpls_route *rt, int flags)
  1427. {
  1428. struct net_device *dev;
  1429. struct nlmsghdr *nlh;
  1430. struct rtmsg *rtm;
  1431. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1432. if (nlh == NULL)
  1433. return -EMSGSIZE;
  1434. rtm = nlmsg_data(nlh);
  1435. rtm->rtm_family = AF_MPLS;
  1436. rtm->rtm_dst_len = 20;
  1437. rtm->rtm_src_len = 0;
  1438. rtm->rtm_tos = 0;
  1439. rtm->rtm_table = RT_TABLE_MAIN;
  1440. rtm->rtm_protocol = rt->rt_protocol;
  1441. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1442. rtm->rtm_type = RTN_UNICAST;
  1443. rtm->rtm_flags = 0;
  1444. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1445. goto nla_put_failure;
  1446. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1447. bool ttl_propagate =
  1448. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1449. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1450. ttl_propagate))
  1451. goto nla_put_failure;
  1452. }
  1453. if (rt->rt_nhn == 1) {
  1454. const struct mpls_nh *nh = rt->rt_nh;
  1455. if (nh->nh_labels &&
  1456. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1457. nh->nh_label))
  1458. goto nla_put_failure;
  1459. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1460. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1461. nh->nh_via_alen))
  1462. goto nla_put_failure;
  1463. dev = rtnl_dereference(nh->nh_dev);
  1464. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1465. goto nla_put_failure;
  1466. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1467. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1468. if (nh->nh_flags & RTNH_F_DEAD)
  1469. rtm->rtm_flags |= RTNH_F_DEAD;
  1470. } else {
  1471. struct rtnexthop *rtnh;
  1472. struct nlattr *mp;
  1473. int dead = 0;
  1474. int linkdown = 0;
  1475. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1476. if (!mp)
  1477. goto nla_put_failure;
  1478. for_nexthops(rt) {
  1479. dev = rtnl_dereference(nh->nh_dev);
  1480. if (!dev)
  1481. continue;
  1482. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1483. if (!rtnh)
  1484. goto nla_put_failure;
  1485. rtnh->rtnh_ifindex = dev->ifindex;
  1486. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1487. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1488. linkdown++;
  1489. }
  1490. if (nh->nh_flags & RTNH_F_DEAD) {
  1491. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1492. dead++;
  1493. }
  1494. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1495. nh->nh_labels,
  1496. nh->nh_label))
  1497. goto nla_put_failure;
  1498. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1499. nla_put_via(skb, nh->nh_via_table,
  1500. mpls_nh_via(rt, nh),
  1501. nh->nh_via_alen))
  1502. goto nla_put_failure;
  1503. /* length of rtnetlink header + attributes */
  1504. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1505. } endfor_nexthops(rt);
  1506. if (linkdown == rt->rt_nhn)
  1507. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1508. if (dead == rt->rt_nhn)
  1509. rtm->rtm_flags |= RTNH_F_DEAD;
  1510. nla_nest_end(skb, mp);
  1511. }
  1512. nlmsg_end(skb, nlh);
  1513. return 0;
  1514. nla_put_failure:
  1515. nlmsg_cancel(skb, nlh);
  1516. return -EMSGSIZE;
  1517. }
  1518. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1519. {
  1520. struct net *net = sock_net(skb->sk);
  1521. struct mpls_route __rcu **platform_label;
  1522. size_t platform_labels;
  1523. unsigned int index;
  1524. ASSERT_RTNL();
  1525. index = cb->args[0];
  1526. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1527. index = MPLS_LABEL_FIRST_UNRESERVED;
  1528. platform_label = rtnl_dereference(net->mpls.platform_label);
  1529. platform_labels = net->mpls.platform_labels;
  1530. for (; index < platform_labels; index++) {
  1531. struct mpls_route *rt;
  1532. rt = rtnl_dereference(platform_label[index]);
  1533. if (!rt)
  1534. continue;
  1535. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1536. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1537. index, rt, NLM_F_MULTI) < 0)
  1538. break;
  1539. }
  1540. cb->args[0] = index;
  1541. return skb->len;
  1542. }
  1543. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1544. {
  1545. size_t payload =
  1546. NLMSG_ALIGN(sizeof(struct rtmsg))
  1547. + nla_total_size(4) /* RTA_DST */
  1548. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1549. if (rt->rt_nhn == 1) {
  1550. struct mpls_nh *nh = rt->rt_nh;
  1551. if (nh->nh_dev)
  1552. payload += nla_total_size(4); /* RTA_OIF */
  1553. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1554. payload += nla_total_size(2 + nh->nh_via_alen);
  1555. if (nh->nh_labels) /* RTA_NEWDST */
  1556. payload += nla_total_size(nh->nh_labels * 4);
  1557. } else {
  1558. /* each nexthop is packed in an attribute */
  1559. size_t nhsize = 0;
  1560. for_nexthops(rt) {
  1561. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1562. /* RTA_VIA */
  1563. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1564. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1565. if (nh->nh_labels)
  1566. nhsize += nla_total_size(nh->nh_labels * 4);
  1567. } endfor_nexthops(rt);
  1568. /* nested attribute */
  1569. payload += nla_total_size(nhsize);
  1570. }
  1571. return payload;
  1572. }
  1573. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1574. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1575. unsigned int nlm_flags)
  1576. {
  1577. struct sk_buff *skb;
  1578. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1579. int err = -ENOBUFS;
  1580. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1581. if (skb == NULL)
  1582. goto errout;
  1583. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1584. if (err < 0) {
  1585. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1586. WARN_ON(err == -EMSGSIZE);
  1587. kfree_skb(skb);
  1588. goto errout;
  1589. }
  1590. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1591. return;
  1592. errout:
  1593. if (err < 0)
  1594. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1595. }
  1596. static int resize_platform_label_table(struct net *net, size_t limit)
  1597. {
  1598. size_t size = sizeof(struct mpls_route *) * limit;
  1599. size_t old_limit;
  1600. size_t cp_size;
  1601. struct mpls_route __rcu **labels = NULL, **old;
  1602. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1603. unsigned index;
  1604. if (size) {
  1605. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1606. if (!labels)
  1607. labels = vzalloc(size);
  1608. if (!labels)
  1609. goto nolabels;
  1610. }
  1611. /* In case the predefined labels need to be populated */
  1612. if (limit > MPLS_LABEL_IPV4NULL) {
  1613. struct net_device *lo = net->loopback_dev;
  1614. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1615. if (!rt0)
  1616. goto nort0;
  1617. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1618. rt0->rt_protocol = RTPROT_KERNEL;
  1619. rt0->rt_payload_type = MPT_IPV4;
  1620. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1621. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1622. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1623. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1624. lo->addr_len);
  1625. }
  1626. if (limit > MPLS_LABEL_IPV6NULL) {
  1627. struct net_device *lo = net->loopback_dev;
  1628. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1629. if (!rt2)
  1630. goto nort2;
  1631. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1632. rt2->rt_protocol = RTPROT_KERNEL;
  1633. rt2->rt_payload_type = MPT_IPV6;
  1634. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1635. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1636. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1637. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1638. lo->addr_len);
  1639. }
  1640. rtnl_lock();
  1641. /* Remember the original table */
  1642. old = rtnl_dereference(net->mpls.platform_label);
  1643. old_limit = net->mpls.platform_labels;
  1644. /* Free any labels beyond the new table */
  1645. for (index = limit; index < old_limit; index++)
  1646. mpls_route_update(net, index, NULL, NULL);
  1647. /* Copy over the old labels */
  1648. cp_size = size;
  1649. if (old_limit < limit)
  1650. cp_size = old_limit * sizeof(struct mpls_route *);
  1651. memcpy(labels, old, cp_size);
  1652. /* If needed set the predefined labels */
  1653. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1654. (limit > MPLS_LABEL_IPV6NULL)) {
  1655. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1656. rt2 = NULL;
  1657. }
  1658. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1659. (limit > MPLS_LABEL_IPV4NULL)) {
  1660. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1661. rt0 = NULL;
  1662. }
  1663. /* Update the global pointers */
  1664. net->mpls.platform_labels = limit;
  1665. rcu_assign_pointer(net->mpls.platform_label, labels);
  1666. rtnl_unlock();
  1667. mpls_rt_free(rt2);
  1668. mpls_rt_free(rt0);
  1669. if (old) {
  1670. synchronize_rcu();
  1671. kvfree(old);
  1672. }
  1673. return 0;
  1674. nort2:
  1675. mpls_rt_free(rt0);
  1676. nort0:
  1677. kvfree(labels);
  1678. nolabels:
  1679. return -ENOMEM;
  1680. }
  1681. static int mpls_platform_labels(struct ctl_table *table, int write,
  1682. void __user *buffer, size_t *lenp, loff_t *ppos)
  1683. {
  1684. struct net *net = table->data;
  1685. int platform_labels = net->mpls.platform_labels;
  1686. int ret;
  1687. struct ctl_table tmp = {
  1688. .procname = table->procname,
  1689. .data = &platform_labels,
  1690. .maxlen = sizeof(int),
  1691. .mode = table->mode,
  1692. .extra1 = &zero,
  1693. .extra2 = &label_limit,
  1694. };
  1695. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1696. if (write && ret == 0)
  1697. ret = resize_platform_label_table(net, platform_labels);
  1698. return ret;
  1699. }
  1700. #define MPLS_NS_SYSCTL_OFFSET(field) \
  1701. (&((struct net *)0)->field)
  1702. static const struct ctl_table mpls_table[] = {
  1703. {
  1704. .procname = "platform_labels",
  1705. .data = NULL,
  1706. .maxlen = sizeof(int),
  1707. .mode = 0644,
  1708. .proc_handler = mpls_platform_labels,
  1709. },
  1710. {
  1711. .procname = "ip_ttl_propagate",
  1712. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  1713. .maxlen = sizeof(int),
  1714. .mode = 0644,
  1715. .proc_handler = proc_dointvec_minmax,
  1716. .extra1 = &zero,
  1717. .extra2 = &one,
  1718. },
  1719. {
  1720. .procname = "default_ttl",
  1721. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  1722. .maxlen = sizeof(int),
  1723. .mode = 0644,
  1724. .proc_handler = proc_dointvec_minmax,
  1725. .extra1 = &one,
  1726. .extra2 = &ttl_max,
  1727. },
  1728. { }
  1729. };
  1730. static int mpls_net_init(struct net *net)
  1731. {
  1732. struct ctl_table *table;
  1733. int i;
  1734. net->mpls.platform_labels = 0;
  1735. net->mpls.platform_label = NULL;
  1736. net->mpls.ip_ttl_propagate = 1;
  1737. net->mpls.default_ttl = 255;
  1738. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1739. if (table == NULL)
  1740. return -ENOMEM;
  1741. /* Table data contains only offsets relative to the base of
  1742. * the mdev at this point, so make them absolute.
  1743. */
  1744. for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
  1745. table[i].data = (char *)net + (uintptr_t)table[i].data;
  1746. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1747. if (net->mpls.ctl == NULL) {
  1748. kfree(table);
  1749. return -ENOMEM;
  1750. }
  1751. return 0;
  1752. }
  1753. static void mpls_net_exit(struct net *net)
  1754. {
  1755. struct mpls_route __rcu **platform_label;
  1756. size_t platform_labels;
  1757. struct ctl_table *table;
  1758. unsigned int index;
  1759. table = net->mpls.ctl->ctl_table_arg;
  1760. unregister_net_sysctl_table(net->mpls.ctl);
  1761. kfree(table);
  1762. /* An rcu grace period has passed since there was a device in
  1763. * the network namespace (and thus the last in flight packet)
  1764. * left this network namespace. This is because
  1765. * unregister_netdevice_many and netdev_run_todo has completed
  1766. * for each network device that was in this network namespace.
  1767. *
  1768. * As such no additional rcu synchronization is necessary when
  1769. * freeing the platform_label table.
  1770. */
  1771. rtnl_lock();
  1772. platform_label = rtnl_dereference(net->mpls.platform_label);
  1773. platform_labels = net->mpls.platform_labels;
  1774. for (index = 0; index < platform_labels; index++) {
  1775. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1776. RCU_INIT_POINTER(platform_label[index], NULL);
  1777. mpls_notify_route(net, index, rt, NULL, NULL);
  1778. mpls_rt_free(rt);
  1779. }
  1780. rtnl_unlock();
  1781. kvfree(platform_label);
  1782. }
  1783. static struct pernet_operations mpls_net_ops = {
  1784. .init = mpls_net_init,
  1785. .exit = mpls_net_exit,
  1786. };
  1787. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  1788. .family = AF_MPLS,
  1789. .fill_stats_af = mpls_fill_stats_af,
  1790. .get_stats_af_size = mpls_get_stats_af_size,
  1791. };
  1792. static int __init mpls_init(void)
  1793. {
  1794. int err;
  1795. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1796. err = register_pernet_subsys(&mpls_net_ops);
  1797. if (err)
  1798. goto out;
  1799. err = register_netdevice_notifier(&mpls_dev_notifier);
  1800. if (err)
  1801. goto out_unregister_pernet;
  1802. dev_add_pack(&mpls_packet_type);
  1803. rtnl_af_register(&mpls_af_ops);
  1804. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1805. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1806. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1807. rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
  1808. mpls_netconf_dump_devconf, NULL);
  1809. err = 0;
  1810. out:
  1811. return err;
  1812. out_unregister_pernet:
  1813. unregister_pernet_subsys(&mpls_net_ops);
  1814. goto out;
  1815. }
  1816. module_init(mpls_init);
  1817. static void __exit mpls_exit(void)
  1818. {
  1819. rtnl_unregister_all(PF_MPLS);
  1820. rtnl_af_unregister(&mpls_af_ops);
  1821. dev_remove_pack(&mpls_packet_type);
  1822. unregister_netdevice_notifier(&mpls_dev_notifier);
  1823. unregister_pernet_subsys(&mpls_net_ops);
  1824. }
  1825. module_exit(mpls_exit);
  1826. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1827. MODULE_LICENSE("GPL v2");
  1828. MODULE_ALIAS_NETPROTO(PF_MPLS);